如何仅限制.cor文件添加到列表中。 下面的代码允许将.corx,.corxx,.corxxx添加到列表中。 我只想要.cor文件。这可能吗?
private void btn_models_Click(object sender, EventArgs e)
{
DialogResult res = dlg_find_folder.ShowDialog();
if (res == DialogResult.OK)
{
tbx_models.Text = dlg_find_folder.SelectedPath;
populateChecklist(tbx_models.Text, "cor");
cbx_all.CheckState = System.Windows.Forms.CheckState.Checked;
}
}
/// <summary>
/// Function populates the models checklist based on the models found in the specified folder.
/// </summary>
/// <param name="directory">Directory in which to search for files</param>
/// <param name="extension">File extension given without period</param>
private void populateChecklist(String directory, String extension)
{
clb_run_list.Items.Clear();
System.Collections.IEnumerator enumerator;
String mdl_name;
try
{
enumerator = System.IO.Directory.GetFiles(directory, "*." + extension).GetEnumerator();
while (enumerator.MoveNext())
{
mdl_name = parse_file_name((String)enumerator.Current, directory, extension);
clb_run_list.Items.Add(mdl_name);
}
}
catch
{
//above code will fail if the initially specified directory does not exist
//MessageBox.Show("The specified directory does not exist. Please select a valid directory.");
}
return;
}
答案 0 :(得分:4)
怎么样;
if (Path.GetExtension(mdl_name).Equals(".cor", StringComparison.OrdinalIgnoreCase))
clb_run_list.Items.Add(mdl_name);
答案 1 :(得分:3)
在添加到您的列表之前检查FileName.EndsWith(extension)
?
答案 2 :(得分:2)
这是Windows支持旧DOS 8.3文件名的工件。扩展名为.corxxx的文件会映射到像Blah~1.cor这样的8.3名称。并且会匹配您的通配符。
你无能为力,只需仔细检查你得到的文件名。使用Path.GetExtension()