First of all, the code seems to work fine. Don't know what your problem is. I changed the file type I was looking for to ".cs" since I have no .acc. Also since you were bothering with the list of files (
Filenames
) instead of
Filename
, I set multiselect to true:
System.Windows.Forms.OpenFileDialog os = new System.Windows.Forms.OpenFileDialog
{
Filter = "Audio (*.cs,*.acc,*wma)|*.cs;*.mp3;*.wma|All Files (*.*)|*.*",
Multiselect = true,
FilterIndex = 1
};
var result = os.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
foreach (var file in os.FileNames)
{
if (System.IO.Path.GetExtension(file) == ".cs")
{
MessageBox.Show("yes it contain");
}
}
}
os.Dispose();
I used the object initializer since that generally is recommended, and also use the Path class to get the extension.
You made a real serious mistake when you disposed of the dialog box inside the if statement. This meant that if the DialogResult was not "OK" you did not dispose of the DialogBox. Big no no.