如何预先选择" SaveFileDialog中的文件

时间:2014-08-18 20:41:07

标签: c# wpf savefiledialog

我经常在SaveFileDialog中发现用户想要保存到的文件已被选中,而他们所要做的就是点击输入。我想在我的程序中创建此功能。

这是我目前的尝试:

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

我认为我的问题是每当我尝试GetCurrentDirectory时,它都会返回程序位置(Debug文件夹),而不是打开的保存位置。

如何在.cct扩展名的位置预先选择该文件?

为清晰起见而更新

我认为可以清楚地注意到SaveFileDialog始终打开到您在该程序中保存文件的最后一个目录。这是我想要使用的目录。这就像当你覆盖你正在处理的文件时会发生什么。

3 个答案:

答案 0 :(得分:2)

解决问题的关键是在打开SaveFileDialog之前,为InitialDirectory属性赋予一个值。

当然,第一次调用此SaveFileDialog时,没有选择文件夹的先前记录。因此,您可以将此值指向一个众所周知的文件夹,例如MyDocuments

第一次调用后,您可以获取选择的路径并将其保存在配置文件的预定义设置中。现在,当再次进行调用时,您可以简单地检索此值并将其应用于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");
    }
}

答案 1 :(得分:0)

呃......你意识到它是你的应用程序产生了SaveFileDialog,不是吗?

因此,每次显示保存对话框时,只需通过查询FileDialog.FileName属性(http://msdn.microsoft.com/en-us/library/microsoft.win32.filedialog.initialdirectory(v=vs.110).aspx)记住保存文件的位置,然后从文件名中解析目录路径。

您可以将其保存在一些方便的位置,每次显示保存文件对话框时,都可以根据该路径设置初始目录。

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

我不会在这里放任何代码,因为我认为这是一个设计方法问题而不是“show-me-the-code”问题。如果您需要我通过代码说明(希望不是!)

,请告诉我

答案 2 :(得分:0)

要解决我的问题,基本上我所要做的就是存储最后选择的文件名。我决定,因为SaveFileDialog默认情况下已经打开了上次访问的路径,所以在程序的第一次运行中,我会允许用户选择他们想要保存的文件。然后我会存储文件路径,保存后,如果存在文件名,我会将新的dlg.FileName设置为存储的路径。

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('\\');
}

感谢大家的帮助!