Hi folks,

I was wondering how I can differentiate between file types when using OpenFileDialog. I would like to be able to have the option to load different file types (as denoted by the Filter property) and handle each type in a different way.

For example

Stream stream;

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.CheckFileExists = true;
            openFileDialog.CheckPathExists = true;
            openFileDialog.InitialDirectory = Application.StartupPath;
            openFileDialog.Filter = "Foo Files (*.foo)|*.foo|Bar Files (*.bar)|*.bar";
            openFileDialog.FilterIndex = 2;
            openFileDialog.RestoreDirectory = true; 

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((stream = openFileDialog.OpenFile()) != null)
                    {
                        XmlReaderSettings settings = new XmlReaderSettings();
                        settings.ConformanceLevel = ConformanceLevel.Auto;
                        settings.IgnoreComments = true;

                        using (XmlReader reader = XmlReader.Create(new StreamReader(stream, System.Text.Encoding.UTF8), settings))
                        {
				// If Foo File do this

				
				// If Bar File do this

                        }

                        stream.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }

How can I determine what type of file has been loaded and process it accordingly?

Recommended Answers

All 2 Replies

openFileDialog.FileName returns a string representing the full path of the selected file including the extension, since it's a string you can use the static String method Contains to check if the file name coantians .foo or .bar, like this:

if (openFileDialog.FileName.Contains(".foo"))
            {
                MessageBox.Show("You opened a .foo file");
            }
            else if (openFileDialog.FileName.Contains(".bar"))
            {
                MessageBox.Show("You opened a .bar file");
            }

Or use the static GetExtension method of the Path class like this:

if (System.IO.Path.GetExtension(openFileDialog.FileName).ToLower() == ".foo")
            {
                MessageBox.Show("You opened a .foo file");
            }
            else if (System.IO.Path.GetExtension(openFileDialog.FileName).ToLower() == ".bar")
            {
                MessageBox.Show("You opened a .bar file");
            }

Note: Before this code make sure you set openFileDialog.FileName to an empty string so if you open the openFileDialog twice and you click Cancel it won't execute the code inside the if/else statement again, like this:

openFileDialog.FileName = String.Empty
or
openFileDialog.FileName = ""

Maybe there's a better way to check the file extension, I'm not sure, but this how I do it...

commented: Straight to the point and well worded. +3

Perfect! Thank you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.