如何让我的应用程序存储在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();
}
答案 0 :(得分:9)
更改设置后,您必须致电
Settings.Default.Save();
然后在打开OpenFileDialog之前设置
openFileDialog1.InitialDirectory = Settings.Default.acc_path;
答案 1 :(得分:9)
这是最简单的方法:FileDialog.RestoreDirectory。
答案 2 :(得分:4)
我认为使用SetCurrentDirectory来操作操作系统的当前目录就足够了。因此,在下一个对话框打开时,它将选择该路径。
或者只是将路径保存到应用程序的某个变量中并使用
FileDialog.InitialDirectory财产。
答案 3 :(得分:2)
以下是您在应用程序生命周期内确保OpenFileDialog将在用户上次选择的目录中打开所需的全部内容。
OpenFileDialog OpenFile = new OpenFileDialog();
OpenFile.RestoreDirectory = false;
答案 4 :(得分:1)
您可以使用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();
}
答案 5 :(得分:1)
我发现你所要做的就是不设置初始目录,对话框会记住你上次保存/打开的位置。即使在应用程序关闭并重新打开后,这也会记住。在初始目录中注释掉这个代码。上面的许多建议也可以使用,但如果您不想寻找其他功能,那么您就必须这样做。
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);
}
}
}
答案 6 :(得分:0)
如果您使用
Dim myFileDlog As New OpenFileDialog()
然后你可以用它来恢复最后一个目录
myFileDlog.RestoreDirectory = True
这不是
myFileDlog.RestoreDirectory = False
(在VB.NET中)
答案 7 :(得分:0)
我知道这是一个旧线程,但我无法找到我喜欢的同一个问题的解决方案,所以我开发了自己的。我在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
属性设置为存储在配置文件中的属性。希望这有助于任何一个谷歌搜索。
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));
}
}