50

I'm using SaveFileDialog.SaveFile. How can I get it to the default (operating system) drive letter and also limit the options to show only .BIN as the file extension?

I tried reading the docs on MSDN but I'm very new to this and to be honest I find them sometimes unclear.

6
  • 7
    Neither of your questions make sense.
    – SLaks
    Feb 27, 2011 at 21:55
  • 1
    I think the first question is meant to read: "How do you find out which drive letter the OS is installed on?"
    – user142162
    Feb 27, 2011 at 21:58
  • thats right Tim Cooper that is what I was meaning to say.
    – user636438
    Feb 27, 2011 at 22:09
  • Possible duplicate of Obtain file path of C# save dialog box
    – NoWar
    Dec 11, 2015 at 18:03
  • I have edited the question text to try and make it more clear - this was based on the OP's comments around different parts of this overall question Jul 18, 2018 at 13:39

3 Answers 3

107

The SaveFileDialog control won't do any saving at all. All it does is providing you a convenient interface to actually display Windows' default file save dialog.

  1. Set the property InitialDirectory to the drive you'd like it to show some other default. Just think of other computers that might have a different layout. By default windows will save the directory used the last time and present it again.

  2. That is handled outside the control. You'll have to check the dialog's results and then do the saving yourself (e.g. write a text or binary file).

Just as a quick example (there are alternative ways to do it). savefile is a control of type SaveFileDialog

SaveFileDialog savefile = new SaveFileDialog(); 
// set a default file name
savefile.FileName = "unknown.txt";
// set filters - this can be done in properties as well
savefile.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";

if (savefile.ShowDialog() == DialogResult.OK)
{
    using (StreamWriter sw = new StreamWriter(savefile.FileName))
        sw.WriteLine ("Hello World!");
}
8
  • I understand that the SaveFileDLg wont provide the functionality of saving my binary file, I just neededto know how to find default drive leter and limit the option on my SaveFileDialog to show only .BIN as the file extension
    – user636438
    Feb 27, 2011 at 22:15
  • Just like your filter shown about for .txt I couldnt find out how to make mine .bin
    – user636438
    Feb 27, 2011 at 22:15
  • 2
    Just replace it. The format is rather simple - just several segments of the following string "syntax": "display text|filter|" E.g. for your bin files you'd write: "BIN files|*.bin|". The actual filter can be any file mask you'd use with cmd's dir command. To limit it, just provide ONE filter description, but you can add more if you want (up to 256 iirc).
    – Mario
    Feb 27, 2011 at 22:17
  • Since saveFile does not exist in the current context, how do I define that variable? I'm very unfamiliar with this and I found this question. Sorry to necropost btw but I'd appreciate the help. Thanks :)
    – puretppc
    Jan 10, 2014 at 5:07
  • It's a OpenFileDialog or SaveFileDialog object, which can either be created at runtime or put as a component on your form (it's in the Toolbox under "Dialogs").
    – Mario
    Jan 10, 2014 at 10:25
5

Environment.GetSystemVariable("%SystemDrive%"); will provide the drive OS installed, and you can set filters to savedialog Obtain file path of C# save dialog box

2
  • Thank you thats exactly what I needed, but how can I limit the the filtre to only show .bin
    – user636438
    Feb 27, 2011 at 22:13
  • I cant select to mark your solution as the answer, not enough points but thank you very much adt, you and Mario only ones understood me so thank you :)
    – user636438
    Feb 27, 2011 at 22:24
3

Here's an example that actually filters for BIN files. Also Windows now want you to save files to user locations, not system locations, so here's an example (you can use intellisense to browse the other options):

            var saveFileDialog = new Microsoft.Win32.SaveFileDialog()
            {
                DefaultExt = "*.xml",
                Filter = "BIN Files (*.bin)|*.bin",
                InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
            };

            var result = saveFileDialog.ShowDialog();
            if (result != null && result == true)
            {
                // Save the file here
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.