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?