如何限制僅將.cor文件添加到列表中。 下面的代碼允許將.corx,.corxx,.corxxx添加到列表中。 我只想要.cor文件。那可能嗎?文件擴展限制爲.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;
}
問題出在parse_file_name方法中(不是由我創建的)。抵消是錯誤的。所以,一旦我拿出來,並用你的建議事情開始看起來更明亮......我能夠弄清楚謝謝所有尤其是伊恩...... – MicroSumol 2010-06-24 18:35:55