Search code examples
c#winformsopenfiledialog

JSON files aren't shown in Open File Dialog while in the filter


I've created an OpenFileDialog in C# and set its filter to this snippet:

OpenFileDialog openDailog = _MainForm.openFileDialog1; openDailog.Filter = "Json files (*.json) | *.json |Text files (*.txt)|*.txt";

The problem is that it doesn't show JSON files but text files are shown in the windows. Is this filter wrong for JSON files or something else?


Solution

  • You have spaces in your Filter, and the filter is very sensitive to spaces. Now it matches on [SPACE]*.json[SPACE], not *.json.

    Remove the spaces and you'll be fine:

    openDailog.Filter = "Json files (*.json)|*.json|Text files (*.txt)|*.txt";