开发者

File extension restriction to .cor (only)

开发者 https://www.devze.com 2023-01-04 10:42 出处:网络
How can I restrict only .cor files to be added to the list. The code bellow allows .corx, .corxx, .corxxx to be added to the list.

How can I restrict only .cor files to be added to the list. The code bellow allows .corx, .corxx, .corxxx to be added to the list. I only want .cor files. Is that possible?

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_model开发者_C百科s.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;
}


How about;

if (Path.GetExtension(mdl_name).Equals(".cor", StringComparison.OrdinalIgnoreCase))
  clb_run_list.Items.Add(mdl_name);


Do a check for FileName.EndsWith(extension) before adding to your list?


This is an artifact of Windows support for old DOS 8.3 filenames. Files with an extension like .corxxx get mapped to a 8.3 name like Blah~1.cor. And will match your wildcard.

Nothing you can do but double-check the filename you get. Use Path.GetExtension()

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号