I'm trying to read a list of documents in a directory. I understand how the filter modifiers work at a high level and have used them before. However, this time I need to filter something that doesn't have a standardized extension (i.e. "*.txt" or *.pdf").

I'm using the example as found on.... https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.filedialog.filter?view=net-5.0

 using System;

using System.Windows.Forms;

namespace COMS2412_Readin
    public partial class Form1 : Form
        string Serial_num = null;
        string INI_file_dir_temp = null;
        string CONFIG_data = null; 
        public Form1()

        private void Form1_Load(object sender, EventArgs e)
            //Allow user to enter serial number and load it for useage later. 
            Serial_num = Serial_number_text.Text;
                using (OpenFileDialog COMS_file_dialog = new OpenFileDialog())
                    COMS_file_dialog.InitialDirectory = "c:\\";
                    //Have dialog box filter out all files without the .INI extention. 
                    COMS_file_dialog.Filter = "txt files (*.INI)|*.INI|All files (*.*)|*.*"; //Failed
                    //                    COMS_file_dialog.Filter = "*.INI"; //Failed
                    //                    COMS_file_dialog.Filter = "(*.INI)"; //Failed
                    //                    COMS_file_dialog.Filter = "(INI files | *.INI)"; //Failed
                    COMS_file_dialog.FilterIndex = 2;
                    COMS_file_dialog.RestoreDirectory = true;
                    if (COMS_file_dialog.ShowDialog() == DialogResult.OK)
                        //Check to make sure it was the correct file type. 
                        INI_file_dir_temp = COMS_file_dialog.FileName;
                //Sorry there was an error with opening the file. Please ensure the file is not currently open. 

The project I'm working on imports config files from an external piece of equipment. The problem is there are literally thousands of files, each with unusual file extensions. For example *.INI is one of the extensions I want to filter. I've tried just entering `*.INI as the argument but it didn't work. What am I missing? Is there a way to alter...

openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

to filter specific file extensions?

  • For some reason, Stackoverflow edited my entry and omitted the asterisk in front of the "*.INI." I understand to do file searches based on file extension, even in explore, this asterisk is required. – BBridges Mar 17 at 21:43
  • 2
    You are posting the code from documentation but ask us to fix your code where you use *.INI? Why don't you post the real code only? Answering your comment, you need to wrap your code in ticks(or use the code-button in the editor) to avoid that it will be interpreted in some way. I have edited your question accordingly. – Tim Schmelter Mar 17 at 21:49
  • Thank you! I will remember that in the future. As far as the exact code, it is this same block of code just with different variable names to compliment my data set. I assumed it would aid in the readability if I used a familiar block of code with familiar variable names. I'm sorry if my assumption was incorrect. – BBridges Mar 17 at 21:56
  • 1
    Just edit your question and replace it with the real code. Or at least add it after the copy&pasted code from the docs. – Tim Schmelter Mar 17 at 22:04
  • What do you mean by "standardized extensions" and how does the code you've used "fail"? Do you mean that you have files in a directory which are in the INI format, but don't have an .ini extension? – CodeCaster Mar 17 at 22:37

Finally, have something that works...

openFileDialog.Filter = "(*.INI)|*.INI";

This filters out everything that isn't of the .INI extension. For me specifically, I had to run VS as admin and ensure the files were not hidden.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.