tags:

views:

51

answers:

3

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_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;
        }
+3  A: 

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

Ian Jacobs
The problem was in the parse_file_name method (not created by me). The offset was wrong. So once I took that out, and used your recommendation things started to look brighter... I was able to figure it out Thanks To All especially Ian....
microsumol
+4  A: 

How about;

if (Path.GetExtension(mdl_name).Equals(".cor", StringComparison.OrdinalIgnoreCase))
  clb_run_list.Items.Add(mdl_name);
Alex K.
+2  A: 

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()

Hans Passant
Yep, indeed it is very interesting to read the remarks (http://msdn.microsoft.com/en-us/library/wz42302f.aspx). Only extensions with three chars are handled like this. And only with the `*` wildchar. Not quite what I call intuitive.
tanascius