Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i open dialog box to select audio file's everything except ".acc" can be seen and selected.. Following is my code..
C#
{   System.Windows.Forms.OpenFileDialog os = new System.Windows.Forms.OpenFileDialog();

   os.Filter = "Audio (*.mp3,*.acc,*wma)|*.acc;*.mp3;*.wma|All Files (*.*)|*.*";
   os.FilterIndex = 1;
   System.Windows.Forms.DialogResult result = os.ShowDialog();

   if (result == System.Windows.Forms.DialogResult.OK)
   {   os.Dispose();
       string[] path = os.FileNames;
       for (int j = 0; j < path.Length; j++)
        {
          string s=path[j];
         if(s.Contains(".acc")) // 2nd if
           {
               MessageBox.Show("yes it contain");
           }
         }
  }

       this.Activate();
       this.Topmost = true;
       this.Topmost = false;
       this.Focus();

}//end

when i choose all Files that time it is seen but again it does not enter into 2nd if even if file is .acc
thankx in advance
Posted
Updated 8-Jun-12 8:40am
v2

1 solution

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:

C#
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") // 2nd if
        {
            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.
 
Share this answer
 
Comments
mane0806 8-Jun-12 23:45pm    
In my code i had mentioned multiselect=true i just forgot to write here..see in that dialogbox all types of file can be seen bt except .acc audio file.. i guess windows to dont detect .acc file coz i searched in drives bye typing ".acc" they didnt show me anything but when i searched by typing ".mp3" all files were shown...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900