質問

I have an OpenFileDialog with the filter set to *.wav. However, when I execute the OpenFileDialog it also shows other files that includes .wav but the true extension is not .wav but e.g. png. Why is that and how can I avoid this?

Right now I take care of it when loading the file(s) for processing but I would like to avoid getting them in the OpenFileDialog list in the first place. Is this a bug in the control or is it me?

Background: I had by accident renamed a picture file to TheFile.wav.png - stupid, true, but these kind of things happens also for other users.

Thanks in advance

役に立ちましたか?

解決

Try this to set the file type in the dialog:

OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "wav files (*.wav)|*.wav";
dialog.InitialDirectory = @"C:\";  // You may not need this.
if (dialog.ShowDialog() == DialogResult.OK)  // Or this; I was just being thorough.
{
   //  Your code can go here.
} 

Just make sure that when using the .Filter property, you follow the pattern I have above, or else it won't work. Also, as was mentioned above, you may want to do some validation after the user selects something.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top