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.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
user636438
  • 531
  • 1
  • 4
  • 7
  • 7
    Neither of your questions make sense. – SLaks Feb 27 '11 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?" –  Feb 27 '11 at 21:58
  • thats right Tim Cooper that is what I was meaning to say. – user636438 Feb 27 '11 at 22:09
  • Possible duplicate of [Obtain file path of C# save dialog box](http://stackoverflow.com/questions/180330/obtain-file-path-of-c-sharp-save-dialog-box) – NoWar Dec 11 '15 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 – StayOnTarget Jul 18 '18 at 13:39
  • There is a good example [SaveFileDialog](https://www.c-sharpcorner.com/UploadFile/mahesh/savefiledialog-in-C-Sharp/) explaining how to use it very clear. – 劉鎮瑲 Feb 18 '19 at 01:37

3 Answers3

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!");
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Mario
  • 35,726
  • 5
  • 62
  • 78
  • 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 '11 at 22:15
  • Just like your filter shown about for .txt I couldnt find out how to make mine .bin – user636438 Feb 27 '11 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 '11 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 '14 at 05: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 '14 at 10:25
  • Does anyone know where Windows stores the last used directories of an application? – Kevin Vuilleumier Sep 17 '14 at 07:14
  • @KevinVuilleumier You should ask such things in a new quesiton. However, you should be able to find the MRU split by file extension under `HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidlMRU` (note that it's not in clear text). – Mario Sep 17 '14 at 07:22
  • Is there any way to get the save file dialog which is already opened ? like we do with forms Application.OpenForms – Jay Aug 13 '20 at 10:48
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

Community
  • 1
  • 1
adt
  • 4,320
  • 5
  • 35
  • 54
  • Thank you thats exactly what I needed, but how can I limit the the filtre to only show .bin – user636438 Feb 27 '11 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 '11 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
            }
Robin Bennett
  • 3,192
  • 1
  • 8
  • 18