简体   繁体   中英

Cannot filter pdfs and txt files in OpenFileDialog

I have been using this filter to filter pdfs and other files.

 ChooseDocumnetOfd.Filter = "Pdf files (*.pdf)|*.pdf |Office Files|*.doc;*.xls;*.ppt |Txt files (*.txt); *.txt | ";

It had been working well for a long time. However, I can only filter Word files now. Why can't I filter any other type of file now?

That's not a valid filter string; you're missing a pipe symbol after the Txt files entry, and there's an extraneous pipe at the end. It should look like this:

ChooseDocumnetOfd.Filter = "Pdf files (*.pdf)|*.pdf|Office Files|*.doc;*.xls;*.ppt|Txt files (*.txt)|*.txt";

I'd have written:

 ChooseDocumnetOfd.Filter = "Pdf files (*.pdf)|*.pdf|Office files (*doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt|Text files (*.txt)|*.txt";

Ive no idea explanation as to why it was working before; what you have in your question doesn't follow the rules laid out in the documentation ( https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.filedialog.filter?view=netframework-4.7.2 )

The basic rule is:

description1|extensionlist1|description2|extensionlist2...

Multiple extensions delimited by semicolon. Descriptions can contain any text and don't have to contain the extension list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM