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.

Ed Marty
  • 39,590
  • 19
  • 103
  • 156
  • 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. – shabby Feb 02 '12 at 05:27
  • You can use SaveFileDialog.Filter or SaveFileDialog.FilterIndex to retrieve the info you need. – Ed Marty Feb 02 '12 at 14:49
  • 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? – BeemerGuy Oct 18 '12 at 05:49
  • 1
    I'm fairly certain this was not the case. I always turn that off immediately when I get a new computer. – Ed Marty Oct 19 '12 at 00:33

5 Answers5

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.

BeemerGuy
  • 8,139
  • 2
  • 35
  • 46
  • 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" – Ed Marty Nov 08 '10 at 22:55
2

Adding DefaultExt and AddExtension will give you the behaviour you're looking for. Simialr to question/answer provided here: https://stackoverflow.com/a/1213353/101971

        var saveFileDialog = new SaveFileDialog
                                 {
                                     Filter = "Foo (*.foo)|*.foo|Bar (*.bar)|*.bar",
                                     DefaultExt = "foo",
                                     AddExtension = true
                                 };
Community
  • 1
  • 1
MEF2A
  • 238
  • 1
  • 5
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

CodeMonkey1313
  • 15,717
  • 17
  • 76
  • 109
  • 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. – Ed Marty Nov 08 '10 at 22:53
  • 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? – shabby Feb 02 '12 at 05:28
0

You may do: savefiledialog1.AddExtension = True

Ahmed Suror
  • 439
  • 6
  • 17
  • This doesn't solve my particular problem, which was to change the displayed filename in the save box. – Ed Marty Jun 17 '13 at 17:35
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!");
  }
}
Tyler2P
  • 2,324
  • 26
  • 22
  • 31