Change file extension when user changes Save As Type in SaveFileDialog
Asked Answered
P

5

8

I have a SaveFileDialog with the option of saving with type .foo or .bar. The first item in the list, and selected by default, is .foo. The default filename is "untitled", and the default extension is ".foo". When the SaveFileDialog appears, it puts "untitled" in the file name textbox. I can change it to "untitled.foo" but it still doesn't change the behavior in regards to my problem:

If the user switches to .bar, how can I make the filename change to untitled.bar? There's only two events, neither of which is the one I want, and it doesn't seem to be changing itself.

Portend answered 8/11, 2010 at 22:33 Comment(4)
i am having the same problem, i wonder if its a win32 problem in win xp, its not adding the extension as well and i can have no idea which extension the user chose.Cerise
You can use SaveFileDialog.Filter or SaveFileDialog.FilterIndex to retrieve the info you need.Portend
It just occurred to me what I think is the issue here. You most likely have "Hide extensions for known file types" turned on in Windows' Folder Options. With that checked, it will not display the extensions in the SaveFileDialog, even though it will save with the extension. Is that what's happening with you?Exhortative
I'm fairly certain this was not the case. I always turn that off immediately when I get a new computer.Portend
E
11

Ed,
I just tested and it works just fine.
I did this:

        SaveFileDialog sfd = new SaveFileDialog();

        sfd.FileName = "untitled";
        sfd.Filter = "Text (*.txt)|*.txt|Word Doc (*.doc)|*.doc";
        sfd.ShowDialog();

And it automatically changes the suggested save name depending on the filter I choose.
I used the .NET 2.0 framework.
But I'm on Windows 7, which I think matters, since you see the system's file-save dialog, and the way it's implemented is what matters here.

Exhortative answered 8/11, 2010 at 22:45 Comment(1)
Yeah, that definitely looks like it's not working in XP. In fact, my initial assessment was wrong; it's not appending the file extension at all. It's just showing up as "untitled"Portend
N
2

Adding DefaultExt and AddExtension will give you the behaviour you're looking for. Simialr to question/answer provided here: https://mcmap.net/q/513675/-save-file-with-appropriate-extension-in-a-save-file-prompt

        var saveFileDialog = new SaveFileDialog
                                 {
                                     Filter = "Foo (*.foo)|*.foo|Bar (*.bar)|*.bar",
                                     DefaultExt = "foo",
                                     AddExtension = true
                                 };
Nestle answered 9/11, 2012 at 20:3 Comment(0)
H
1

When you go to actually save the file you can get the file name from the dialog box, then perform the necessary string manipulation from there. The file name is a member of the instance of the SaveFileDialog

Hydrocarbon answered 8/11, 2010 at 22:41 Comment(2)
This is more of a UI thing. I know I can add the file extension, but I want the user to see the file extension changing.Portend
too bad code monkey, apart from ed marty's argument, what if you want to save the file in a different manner depending upon the extension the user chooses?Cerise
R
0

You may do: savefiledialog1.AddExtension = True

Ramonramona answered 17/6, 2013 at 15:40 Comment(1)
This doesn't solve my particular problem, which was to change the displayed filename in the save box.Portend
T
0
//Drag a SaveFileDialog from toolbox, and then...
//Create a button.... let's say this is my button named button1
private void button1_Click(object sender, EventArgs e)
{
  //saveFileDialog1 is the tool you grabbed from toolbox and could be renamed if you want
  //using Filter.. so user will see the extension has been selected by default
  saveFileDialog1.Filter = "Text Document(.txt)|.txt";
  if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  {
    /*saveFileDialog1.Filter = "Text Document(.txt)|.txt";*/
    File.Create(saveFileDialog1.FileName);
    MessageBox.Show("New file created succesfully!");
  } else {
    MessageBox.Show("Unsuccessful!");
  }
}
Trichloromethane answered 2/1, 2023 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.