C# OpenFileDialog 的初始目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18586047/
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
Initial directory for OpenFileDialog
提问by Cas Nouwens
The file dialog has to open the last directory location that was used before it was shut down, but I have no idea how to do this. My colleague only shows me the example of word, when you click "file" it shows the last used files, he told me to use a register or an INI file, which I have never used before.
文件对话框必须打开关闭之前使用的最后一个目录位置,但我不知道如何执行此操作。我的同事只给我展示了word的例子,当你点击“文件”时,它会显示最后使用的文件,他告诉我使用寄存器或INI文件,这是我以前从未使用过的。
Here is the code I am using:
这是我正在使用的代码:
string f_sOudeLocatie = @"D:\path\is\classified";
private void btBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Zoek de CSV file";
fdlg.InitialDirectory = f_sOudeLocatie;
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
tbGekozenBestand.Text = fdlg.FileName;
tbVeranderNaamIn.Text = Path.GetDirectoryName(fdlg.FileName);
f_sOudeLocatie = Path.GetDirectoryName(fdlg.FileName);
f_sSourceFileName = fdlg.FileName;
f_sDestFileName = Path.GetFileName(Path.GetDirectoryName(fdlg.FileName)) + ".csv";
btOpslaan.Enabled = true;
tbVeranderNaamIn.ReadOnly = false;
}
}
采纳答案by No Idea For Name
if you'll create the OpenFileDialog
outside the button click event it should remember the last folder you've been
如果您要OpenFileDialog
在按钮单击事件之外创建它,它应该记住您访问过的最后一个文件夹
string f_sOudeLocatie = @"D:\path\is\classified";
OpenFileDialog fdlg = new OpenFileDialog();
public Form1()
{
InitializeComponent();
fdlg.Title = "Zoek de CSV file";
fdlg.InitialDirectory = f_sOudeLocatie;
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = true;
}
private void btBrowse_Click(object sender, EventArgs e)
{
if (fdlg.ShowDialog() == DialogResult.OK)
{
fdlg.InitialDirectory = fdlg.FileName.Remove(fdlg.FileName.LastIndexOf("\"));// THIS LINE IS IMPORTENT
tbGekozenBestand.Text = fdlg.FileName;
tbVeranderNaamIn.Text = Path.GetDirectoryName(fdlg.FileName);
f_sOudeLocatie = Path.GetDirectoryName(fdlg.FileName);
f_sSourceFileName = fdlg.FileName;
f_sDestFileName = Path.GetFileName( Path.GetDirectoryName(fdlg.FileName) ) + ".csv";
btOpslaan.Enabled = true;
tbVeranderNaamIn.ReadOnly = false;
}
}
回答by Peter K John
You can use registry to store the last directory location. And each time you open the file dialogue, get the value from the registry and set as the default location. When it closed store the location back to registry.
您可以使用注册表来存储最后一个目录位置。并且每次打开文件对话框时,从注册表中获取值并设置为默认位置。当它关闭时,将位置存储回注册表。
This code project article explains you well about reading and writing to registry ReadWriteDeleteFromRegistry
这篇代码项目文章很好地解释了读取和写入注册表 ReadWriteDeleteFromRegistry
If you choose to use INI file, some search will give you examples of how to read and write from INI file
如果您选择使用 INI 文件,一些搜索会为您提供如何读取和写入 INI 文件的示例
回答by Ehsan
You need to set
你需要设置
fdlg.RestoreDirectory = false;
Reason:
原因:
RestoreDirectory property makes sure that the value in Environment.CurrentDirectory will be reset before the OpenFileDialog closes. If RestoreDirectory is set to false, then Environment.CurrentDirectory will be set to whatever directory the OpenFileDialog was last open to. As explained here
RestoreDirectory 属性确保 Environment.CurrentDirectory 中的值将在 OpenFileDialog 关闭之前重置。 如果 RestoreDirectory 设置为 false,则 Environment.CurrentDirectory 将设置为 OpenFileDialog 上次打开的任何目录。正如这里所解释的