C# 如何在openFileDialog中保存最后一个文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16078362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save last folder in openFileDialog?
提问by Kenji
How do I make my application store the last path opened in openFileDialogand after new opening restore it?
如何让我的应用程序商店最后打开的路径openFileDialog和新打开后恢复它?
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
acc_path = openFileDialog1.FileName;
Settings.Default.acc_path = acc_path;
foreach (string s in File.ReadAllLines(openFileDialog1.FileName))
{
accs.Enqueue(s);
}
label2.Text = accs.Count.ToString();
}
采纳答案by virious
This is the easiest way: FileDialog.RestoreDirectory.
这是最简单的方法:FileDialog.RestoreDirectory。
回答by Tigran
I think it would be enough for you to use SetCurrentDirectoryto ste the current directory for the OS. So on the next dialog opening it would pick that path.
我认为您使用SetCurrentDirectory来为操作系统设置当前目录就足够了。因此,在下一个打开的对话框中,它将选择该路径。
Or simply save path into some variable of your application and use
FileDialog.InitialDirectoryproperty.
或者简单地将路径保存到应用程序的某个变量中并使用
FileDialog.InitialDirectory属性。
回答by mathieu
You can use the InitialDirectory property : http://msdn.microsoft.com/fr-fr/library/system.windows.forms.filedialog.initialdirectory.aspx
您可以使用 InitialDirectory 属性:http: //msdn.microsoft.com/fr-fr/library/system.windows.forms.filedialog.initialdirectory.aspx
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.InitialDirectory = previousPath;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
previousPath = Path.GetDirectoryName(openFileDialog1.FileName);
acc_path = openFileDialog1.FileName;
Settings.Default.acc_path = acc_path;
foreach (string s in File.ReadAllLines(openFileDialog1.FileName))
{
accs.Enqueue(s);
}
label2.Text = accs.Count.ToString();
}
回答by mathieu
After changing Settings you have to call
更改设置后,您必须调用
Settings.Default.Save();
and before you open the OpenFileDialog you set
在你打开你设置的 OpenFileDialog 之前
openFileDialog1.InitialDirectory = Settings.Default.acc_path;
回答by TEDSON
The following is all you need to make sure that OpenFileDialog will open at the directory the user last selected, during the lifetime off your application.
以下是确保 OpenFileDialog 将在您的应用程序生命周期内最后一次选择的目录中打开的全部内容。
OpenFileDialog OpenFile = new OpenFileDialog();
OpenFile.RestoreDirectory = false;
回答by JoAMoS
I find that all you have to do is NOT set the initial directory and the dialog box remembers your last save/open location. This remembers even after the application is closed and reopened. Try this code with the initial directory commented out. Many of the suggestions above will also work but if you are not looking for additional functionality this is all you have to do.
我发现您所要做的就是不要设置初始目录,对话框会记住您上次保存/打开的位置。即使在应用程序关闭并重新打开后,这也会记住。在注释掉初始目录的情况下尝试使用此代码。上面的许多建议也适用,但如果您不寻找其他功能,这就是您所要做的。
private void button1_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
//openFileDialog1.InitialDirectory = "c:\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
回答by anonymous
if your using
如果您使用
Dim myFileDlog As New OpenFileDialog()
then you can use this to restore the last directory
然后你可以用它来恢复最后一个目录
myFileDlog.RestoreDirectory = True
and this to not
这不是
myFileDlog.RestoreDirectory = False
(in VB.NET)
(在 VB.NET 中)
回答by David Bentley
I know this is a bit of an old thread, but I was not able to find a solution I liked to this same question so I developed my own. I did this in WPF but it should work almost the same in Winforms.
我知道这是一个旧线程,但我无法找到我喜欢的相同问题的解决方案,所以我开发了自己的解决方案。我在 WPF 中做到了这一点,但它在 Winforms 中的工作方式几乎相同。
Essentially, I use an app.configfile to store my programs last path.
本质上,我使用一个app.config文件来存储我的程序的最后一个路径。
When my program starts I read the config file and save to a global variable. Below is a class and function I call when my program starts.
当我的程序启动时,我读取配置文件并保存到全局变量。下面是我在程序启动时调用的一个类和函数。
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"];
}
}
Next I have a button that opens the file browse dialog and sets the InitialDirectoryproperty to what was stored in the config file. Hope this helps any one googling.
接下来,我有一个按钮可以打开文件浏览对话框并将InitialDirectory属性设置为存储在配置文件中的内容。希望这有助于任何一个谷歌搜索。
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));
}
}

