Eric after dark
Eric after dark

Reputation: 1808

How to "pre-select" a file in a SaveFileDialog

Often times in a SaveFileDialog I find that the file that the user wants to save to is already selected, and all they have to do is hit enter. I would like to create this functionality in my program.

This is my current attempt:

Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); 
dlg.Filter = "CCT files (.cct)|*.cct"; //Filter only .cct extensions

Nullable<bool> result = dlg.ShowDialog(); //Show the SaveFileDialog

directoryName = Directory.GetCurrentDirectory(); //ATTEMPT to get the directory that has been opened
fileList = Directory.GetFiles(@directoryName, "*.cct"); //Put the name of the fill path to the file into string form
dlg.FileName = fileList[0]; //Set SelectedItem to the previous file

I think my problem is that whenever I try to GetCurrentDirectory, it returns the programs location (the Debug folder), and not the save location being opened up.

How do I allow a preselect of the file in the location with .cct as it's extension?

Update for clarity

I think it would clear things up to note that a SaveFileDialog always opens up to the last directory that you saved a file to in that program. This is the directory that I want to work with. It's like what happens when you are overwriting a file that you've been working on.

Upvotes: 1

Views: 1811

Answers (3)

Eric after dark
Eric after dark

Reputation: 1808

To solve my problem, basically all I had to do was store the last selected file name. I decided, that since SaveFileDialog already opens the last visited path by default that in the first run of the program, I would allow the user to select the file that they would like to save to. Then I would store the file path, and upon save, if a file name exists, I would set the new dlg.FileName to the stored path.

string currentFileName;
string[] currentFileName_Array;

Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.Filter = "CCT files (.cct)|*.cct"; //Filter only .cct extensions

if (currentFileName != "" && currentFileName != null) //If a pre-selected filename exists
    dlg.FileName = currentFileName_Array[currentFileName_Array.Count() - 1];

Nullable<bool> result = dlg.ShowDialog();

if (result == true)
{
    currentFileName = dlg.FileName; //Save the selected File Name
    currentFileName_Array = currentFileName.Split('\\');
}

Thanks to everyone for your help!

Upvotes: 0

Steve
Steve

Reputation: 216293

The key to solve your problem is to give, before opening the SaveFileDialog, a value to the InitialDirectory property.

Of course, the first time you call this SaveFileDialog there is no previous record of the choosen folder. So you could point this value to a well known folder like MyDocuments.

After the first call you could get the choosen path and save it in a predefined setting of your configuration file. Now when the call is made again you could simply retrieve this value and apply it to InitialDirectory

// A default folder when no previous one has been saved...
string directoryName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); 
dlg.Filter = "CCT files (.cct)|*.cct"; //Filter only .cct extensions

// Try to get back the previous saved folder... 
if(ConfigurationManager.AppSettings["WorkingDirectory"] != null)
   directoryName =  ConfigurationManager.AppSettings["WorkingDirectory"];
dlg.InitialDirectory = directoryName;

fileList = Directory.GetFiles(directoryName, "*.cct"); 
if(fileList.Length > 0)
{
    // Set the default name to show in the dialog
    dlg.FileName = Path.GetFileName(fileList[0]); 
    Nullable<bool> result = dlg.ShowDialog(); 
    if(result.HasValue && result.Value)
    {
        // Try to insert or update the setting with the choosen path
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
        if(config.AppSettings.Settings["WorkingDirectory"] != null)
            config.AppSettings.Settings["WorkingDirectory"].Value = Path.GetDirectoryName(dlg.FileName);
        else
            config.AppSettings.Settings.Add("WorkingDirectory",Path.GetDirectoryName(dlg.FileName));
        config.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");
    }
}

Upvotes: 2

code4life
code4life

Reputation: 15794

Er... you do realize that it's your app that's spawning the SaveFileDialog, don't you?

So each time you show the save dialog, just remember where the save file was by consulting the FileDialog.FileName property (http://msdn.microsoft.com/en-us/library/microsoft.win32.filedialog.initialdirectory(v=vs.110).aspx), and parse the directory path out of the file name.

You can save that in some handy location, and every time you show the save file dialog, set the initial directory based on that path.

http://msdn.microsoft.com/en-us/library/microsoft.win32.filedialog.initialdirectory(v=vs.110).aspx

I'm not going to put any code here, because I'm of the mind that this is a design-approach issue rather than a "show-me-the-code" issue. Let me know if you need me to illustrate via code (hope not!)

Upvotes: 0

Related Questions