Search code examples
c#winformsopenfiledialog

How to limit which files should be shown in OpenFileDialog?


I used information from here http://msdn.microsoft.com/ru-ru/library/system.windows.forms.openfiledialog(v=vs.110).aspx

this way:

Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

 dlg.DefaultExt = ".xml"; // this is how I get only required extension 
 dlg.Filter = "XML files (*.xml)|*.xml"; // I guess, this should be modified, don't know how.
dlg.InitialDirectory = _directoryName1;
// here we go
Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                string path = dlg.FileName;

In the Initial Directory I have to types of files of same xml extension, which names begin with script-Data... or GeneralParam.... So I need to show in the OpenFileDialog only files, which names begin with script-Data....

I know, that I can notify user, what he has decided wrong file by parsing path, but it isn't good solution for me. Is any other way out here?


Solution

  • you have already set the Filter property. so you can see only .xml files when OpenFileDialog opens. but if you want to filter the filenames to be displayed in OpenFileDialog you can set the FileName property as there is no other option to filter by filename

    Try This:

    dlg.FileName = "script-Data*";