Is it necessary to have the "All files" option in the dialogBox. I tried removing it but it won't compile. gives some sort of error

this.saveFileDialog1.Filter =
"Document Files - (*.txt)|*.txt|" +
"All files (*.*)|*.*";

Tried removing the [+ "All file ] onwards string.

this.saveFileDialog1.Filter =
"Document Files - (*.txt)|*.txt|";

gives the same prob. with openFileDialoag.

Dani AI

Generated

Short answer: the dialog does not require an "All files" entry — the problem here was a syntax mistake, not the missing filter. As found, a stray pipe character placed outside the string literal will produce a compile error; as shows, a single valid filter entry is fine.

The Filter property expects pairs of display text and pattern(s), separated by pipe characters, e.g. DisplayName|Pattern. If you split the string across lines you must concatenate properly; if you leave characters (like |) outside the quotes the compiler will complain. A correct, simple example (different from the samples posted above) looks like:

saveFileDialog.Filter = "Text files (*.txt;*.log)|*.txt;*.log|Images (*.png;*.jpg)|*.png;*.jpg";
saveFileDialog.DefaultExt = "txt";
saveFileDialog.AddExtension = true;
saveFileDialog.FilterIndex = 1;

Troubleshooting checklist:

  • For compile errors: look for unmatched quotes, stray characters outside quotes (extra |, +, etc.), or a missing + when concatenating literals across lines. The compiler message usually points to the exact line and character.
  • For runtime/filter problems: ensure the filter string is well-formed (pairs of DisplayName|Pattern), do not leave a trailing pipe inside the string, and separate multiple patterns with semicolons (e.g. *.png;*.jpg).
  • Use DefaultExt and AddExtension to ensure saved files get the right extension, and FilterIndex to pick the initially selected filter.

Official reference for the exact format and behavior is the FileDialog Filter documentation: Filter property documentation.

Recommended Answers

All 2 Replies

Tried it with saveFileDialog1.Filter = "doc files (*.txt)|*.txt"; without problems.

thanks, just figured it out. was placing an extra char "|" after *.txt

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.