Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 551
Rocketq Avatar asked Jan 12 '23 18:01

Rocketq


2 Answers

Try this.it will help you.if you want to filter file name starting with "script-Data" only sho in your application do this.

OpenFileDialog of = new OpenFileDialog();
of.Filter = "Excel Files|script-Data*.xls;ascript-Data*.xlsx;script-Data*.xlsm";
of.ShowDialog();
like image 122
Elshan Avatar answered Feb 03 '23 11:02

Elshan


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*";
like image 27
Sudhakar Tillapudi Avatar answered Feb 03 '23 11:02

Sudhakar Tillapudi