How to Set Filters for OpenFileDialog and SaveFileDialog in C#

Hi friends! In this article, we’ll be discussing about how you can easily set filters for OpenFileDialog and SaveFileDialog in C# and see some simple examples.

 

What are Filters?

Filters are used in OpenFileDialog and SaveFileDialog in C# to specify the type of files that can be opened or saved.

A filter is a string that indicates which file types should be displayed in the dialog box and contains one or more file types, separated by the pipe symbol “|”.

For instance, you can set the filter to “.txt|.csv” if you only want the user to be able to access or save.txt and.csv files.

 

How to Set a Filter for OpenFileDialog?

To set a filter for OpenFileDialog, you can use the Filter property.

Here is a simple example:

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Text files (*.txt)|*.txt|CSV files (*.csv)|*.csv";

In this example, the filter allows the user to choose to open either a text file or a CSV file.

 

How to Set a Filter for SaveFileDialog?

Similarly, to set a filter for SaveFileDialog, you can use the Filter property.

Here’s a simple example:

SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Text files (*.txt)|*.txt|CSV files (*.csv)|*.csv";

In this example, the filter allows the user to save either a text file or a CSV file.

 

 

Let’s see a Full Example on Using Filters

Based on all the above, let’s see a full example on how we can easily use file filters in the OpenFileDialog and SaveFileDialogs in C#:

using System;
using System.Windows.Forms;

namespace FileFiltersExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // Event handling code for when opening a file
        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Text files (*.txt)|*.txt|CSV files (*.csv)|*.csv";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show("You selected file: " + openFileDialog.FileName);

                // add file handling code here...
            }
        }

        // Event handling code for when saving a file
        private void btnSaveFile_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Text files (*.txt)|*.txt|CSV files (*.csv)|*.csv";

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show("You selected file: " + saveFileDialog.FileName);
             
                // add file handling code here...
            }
        }
    }
}

 


Get the help you need to get started with .NET Programming fast and easy!

Enroll to our online course titled “.NET Programming for Beginners – Windows Forms with C#
(special limited-time discount included in link).

.NET Programming for Beginners - Windows Forms with C# (Online Course)
(Lifetime Access, Live Demos, Q&A and more!)

Learn how to implement Windows Forms projects in .NET using Visual Studio and C#, how to implement multithreading, how to create deployment packages and installers for your .NET Windows Forms apps using ClickOnce in Visual Studio, and more!

Many live demonstrations and downloadable resources included!

Learn More


 

Recommended Online Courses:

 

Read Also:

 

Subscribe to our Newsletter!

 

Rate this article: 1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading...

Reference: SQLNetHub.com (https://www.sqlnethub.com)

© SQLNetHub