我知道這是一個一點舊線程,但我無法找到解決方案我喜歡這個相同的問題,所以我開發了我自己的。我在WPF中這樣做,但它應該在Winforms中幾乎相同。
本質上,我使用app.config
文件來存儲我的程序的最後一個路徑。
當我的程序啓動時,我讀取配置文件並保存到全局變量。下面是我的程序啓動時調用的類和函數。
public static class Statics
{
public static string CurrentBrowsePath { get; set; }
public static void initialization()
{
ConfigurationManager.RefreshSection("appSettings");
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
CurrentBrowsePath = ConfigurationManager.AppSettings["lastfolder"];
}
}
接下來我有打開的文件瀏覽對話框,並設置InitialDirectory
屬性什麼存儲在配置文件中的按鈕。希望這有助於任何人使用Google搜索。
private void browse_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog open_files_dialog = new OpenFileDialog();
open_files_dialog.Multiselect = true;
open_files_dialog.Filter = "Image files|*.jpg;*.jpeg;*.png";
open_files_dialog.InitialDirectory = Statics.CurrentBrowsePath;
try
{
bool? dialog_result = open_files_dialog.ShowDialog();
if (dialog_result.HasValue && dialog_result.Value)
{
string[] Selected_Files = open_files_dialog.FileNames;
if (Selected_Files.Length > 0)
{
ConfigWriter.Update("lastfolder", System.IO.Path.GetDirectoryName(Selected_Files[0]));
}
// Place code here to do what you want to do with the selected files.
}
}
catch (Exception Ex)
{
MessageBox.Show("File Browse Error: " + Environment.NewLine + Convert.ToString(Ex));
}
}
我個人很喜歡這種方法,因爲我沒有在多個表單中重複使用同一個文件選擇器實例。 – Joel