Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the AddExtension property work in C# 2.0?

Tags:

c#

c#-2.0

I want to open a save file dialog, have the user enter a filename, and if they forget the .csv extension, have it tacked on.

It would seem that the SaveFileDialog AddExtension property would work, but its doesn't. I've even set the DefaultExt property to .csv, and still nothing gets tacked on. My file gets saved just fine, but sans extension, so the user can't just double click on the file and have it open in Excel.

I have to be missing something obvious. Here's what I've got

        SaveFileDialog sfd = new SaveFileDialog();
        sfd.DefaultExt = "*.csv";
        sfd.Filter = "Comma Separated(*.csv)|*.*";
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            // Do my file saving
        }
like image 783
Jonathan Beerhalter Avatar asked Dec 23 '08 15:12

Jonathan Beerhalter


1 Answers

Try just using "csv" for the DefaultExt - also, you should be using this (it is IDisposable):

        using (SaveFileDialog sfd = new SaveFileDialog())
        {
            sfd.AddExtension = true;
            sfd.DefaultExt = "csv";
            sfd.Filter = "Comma Separated(*.csv)|*.*";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                // Do my file saving
            }
        }
like image 67
Marc Gravell Avatar answered Sep 22 '22 14:09

Marc Gravell