Click here to Skip to main content
15,879,096 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm trying to only display CSV files when I open the file dialog instead of CSV and XML. I have attempted to do this like this:

C#
private void button_OpenImportFile_Click(object sender, EventArgs e)
        {            
            OpenFileDialog OpenFileDialog_Import = new OpenFileDialog();

            OpenFileDialog_Import.Filter = "CSV files (*.csv)|*.csv";

            if (openFileDialog_Import.ShowDialog() == DialogResult.OK)
            {
                if (Path.GetExtension(openFileDialog_Import.FileName) != ".csv")
                {
                    WoodstarHelpers.cGeneralHelpers.ShowErrorMessage("Only CSV files are permitted for this import type", "Invalid File Format");
                }
                else
                {
                    OpenFileData(openFileDialog_Import.FileName);
                }
            }                       
        }

This should check that they have selected a CSV however ideally i'd only like them to have the option to choose CSV.


What I have tried:

I've tried doing it with a using but that doesn't seem to do the trick either:

C#
private void button_OpenImportFile_Click(object sender, EventArgs e)
        {            
            using (OpenFileDialog OpenFileDialog_Import = new OpenFileDialog())
            {
                OpenFileDialog_Import.Filter = "CSV files (*.csv)|*.csv";

                if (openFileDialog_Import.ShowDialog() == DialogResult.OK)
                {
                    if (Path.GetExtension(openFileDialog_Import.FileName) != ".csv")
                    {
                        WoodstarHelpers.cGeneralHelpers.ShowErrorMessage("Only CSV files are permitted for this import type", "Invalid File Format");
                    }
                    else
                    {
                        OpenFileData(openFileDialog_Import.FileName);
                    }
                }
            }                      
        }
Posted
Updated 27-Jun-22 22:45pm

See FileDialog.Filter Property (System.Windows.Forms) | Microsoft Docs[^]. Also, if I remember correctly the filter index starts from one rather than zero.
 
Share this answer
 
Comments
Southmountain 28-Jun-22 21:38pm    
it is true:

"A value containing the index of the filter currently selected in the file dialog box. The default value is 1."
First off, correct your code so it compiles: C# is case sensitive, so OpenFileDialog_Import is not the same as openFileDialog_Import:
C#
OpenFileDialog OpenFileDialog_Import = new OpenFileDialog();
               ^
               |
...
if (openFileDialog_Import.ShowDialog() == DialogResult.OK)
    ^
    |
You need to do this for every reference, obviously.

If that doesn't fix the problem, then set the FileDialog.FilterIndex Property (System.Windows.Forms) | Microsoft Docs[^] property
 
Share this answer
 
Comments
Will Sewell 28-Jun-22 4:59am    
Woops. Thanks as always Griff!
OriginalGriff 28-Jun-22 5:17am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900