Search code examples
c#.netfilefilteropenfiledialog

C# : openfiledialog filtering issue


i am writing a code when a user click open file , i would like to filter the type of files to .txt only so i did the following code , but it didnt work it didnt show any txt files at all , just an empty folder , here is the code :

        try
        {
            OpenFileDialog o = new OpenFileDialog();
            o.Filter = "Text File | .txt";
            o.InitialDirectory = Application.StartupPath;
            o.ShowDialog();
        }
        catch
        {}

Solution

  • This will only show files named .txt, i.e., a single space (which it might ignore?) followed by a dot and "txt".

    Remove the spaces around the |, and add a wildcard so you match more than one file.

    o.Filter = "Text File|*.txt";