Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SaveFileDialog doesn't show any possible extensions despite using Filter option

Tags:

c#

winforms

I'm creating right now an application in C# which saves data to the file upon clicking a button. The name and locations of the file is defined by the user, and i want the program to automatically add .txt extension to the name, as well as show only 1 possible extensions in "save files as" combobox. I have this code:

SaveFileDialog Dialog1 = new SaveFileDialog();
Dialog1.DefaultExt = "txt";
Dialog1.Filter = "Pliki tekstowe | *.txt";
Dialog1.AddExtension = true;

private void button1_Click(object sender, EventArgs e)
{
    if (Dialog1.ShowDialog() == DialogResult.OK)
    {
        System.IO.Stream fileStream = Dialog1.OpenFile();
        System.IO.StreamWriter sw = new System.IO.StreamWriter(fileStream);
        sw.WriteLine("Writing some text in the file.");
        sw.WriteLine("Some other line.");
        sw.Flush();
        sw.Close();
    }
    this.Close();
}

But whenever i click the button, i have no options to choose from in the combobox, as well as the .txt extensions are not added to the file name in case the extensions is not specified by the user himself. I know i can somehow bypass that by checking if the user gave the proper extensions, and in case he didn't add ".txt" to the name but i really wanted to know, why that piecei of code doesn't function. Any help?

like image 759
human Avatar asked Nov 21 '25 18:11

human


1 Answers

The problem is the space. Just change it to this:

Dialog1.Filter = "Pliki tekstowe|*.txt";

and you should be in business.

Otherwise it's trying to match files of the pattern  *.txt (subtly compared to *.txt) - which you probably don't have.

like image 157
Yuck Avatar answered Nov 24 '25 06:11

Yuck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!