Search code examples
c#.net-2.0windows-xp

Change file extension when user changes Save As Type in SaveFileDialog


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.


Solution

  • 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.