Well the save file dialog, for some reason removes all file extensions that are input to it. No matter what i do or how i set the filename value, it always removes the extension which is incredibly annoying when i need to extension to stay as it is otherwise the file will save without any extension, does anybody know how to get rid of this problem? thanks.

Dani AI

Generated

, the extension usually disappears because the dialog is trying to “help” by applying the currently selected filter’s extension. If you want to keep whatever the user typed (including multi-dotted like .tar.gz), disable auto-adding and enable multi-dotted support. Also remember the filter index is 1-based when you map the selected “Save as type” back to your save logic, which trips people up. (learn.microsoft.com)

Here is a small, robust pattern that preserves the user’s extension but still gives you a safe fallback if they omit one:

using System.IO;
using System.Windows.Forms;

var sfd = new SaveFileDialog
{
    Filter = "All files (*.*)|*.*",
    AddExtension = false,                 // preserve what the user typed
    SupportMultiDottedExtensions = true,  // allow .tar.gz, etc.
    ValidateNames = true,
    CheckPathExists = true,
    OverwritePrompt = true
};

// Optional: seed the file name from a URL like you mentioned, @Webbsta
// sfd.FileName = Path.GetFileName(new Uri(url).LocalPath);

if (sfd.ShowDialog() == DialogResult.OK)
{
    string path = sfd.FileName;
    if (!Path.HasExtension(path))         // user omitted an extension
        path = Path.ChangeExtension(path, "txt");  // choose your default
    // save to 'path'
}

A couple of related gotchas:

  • If you instead want Windows to add/replace the extension based on the selected filter, turn auto-adding back on and read FilterIndex (remember it is 1-based) to decide which encoder/format to use. (learn.microsoft.com)
  • For readers arriving from ASP.NET: you cannot show a SaveFileDialog from a web app. To control the download name, set the Content-Disposition header (e.g., attachment; filename=...) in your controller/handler. (learn.microsoft.com)

If you share a short repro, folks here can tailor the filter and fallback rules to your exact formats.

Recommended Answers

All 8 Replies

Nevermind, i just used a folder browser dialog instead and took my filename from the end of the URL.

Have you try saveFileDialog.AddExtension property? It is indicating whether the dialog box automatically adds an extension to a file name if the user omits the extension.

The extension added to a file name depends on the currently selected file filter.

Yes i did try that, and it did not effect the dialog in the way i wanted.

Please post your code, so that any one can find the solution.

As i said, nevermind because i just swaped using the savefile dialog with a folderbrowserdialog.

For those who find this thread as a result of a search, here's how you set the extensions for a SaveFileDialog.

// Create new SaveFileDialog object
SaveFileDialog DialogSave = new SaveFileDialog();
 
// Default file extension
DialogSave.DefaultExt = "txt";
 
// Available file extensions
DialogSave.Filter = "Text file (*.txt)|*.txt|XML file (*.xml)|*.xml|All files (*.*)|*.*";

Here's where I found the snippet above:

private void mnuFileSave_Click(object sender, EventArgs e)
        {
            if (dlgFileSave.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                foreach (string strFile in dlgFileSave.FileNames)
                {
                    SingleDocument document = new SingleDocument();
                    document.rtbNotice.SaveFile(strFile, RichTextBoxStreamType.PlainText);
                    document.MdiParent = this;
                    document.Show();
                }
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.