C# 如何在openFileDialog中保存最后一个文件夹?
如何使我的应用程序存储在C# 如何在openFileDialog中保存最后一个文件夹?,c#,openfiledialog,C#,Openfiledialog,如何使我的应用程序存储在openFileDialog中打开的最后一个路径,并在新打开后恢复它 OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { acc_path = openFi
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();
}
我认为您可以使用它来存储操作系统的当前目录。因此,在打开下一个对话框时,它将选择该路径 或者只需将路径保存到应用程序的某个变量中并使用
属性。您可以使用InitialDirectory属性:
更改设置后,您必须呼叫
Settings.Default.Save();
在打开OpenFileDialog之前
openFileDialog1.InitialDirectory = Settings.Default.acc_path;
这是最简单的方法:。以下是确保OpenFileDialog在应用程序生命周期内在用户上次选择的目录下打开所需的全部内容
OpenFileDialog OpenFile = new OpenFileDialog();
OpenFile.RestoreDirectory = false;
我发现您所要做的不是设置初始目录,对话框会记住您上次保存/打开的位置。即使在应用程序关闭并重新打开后,此选项仍会记住。请在注释掉初始目录的情况下尝试此代码。上面的许多建议也会起作用,但如果你不想寻找额外的功能,这就是你所要做的
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);
}
}
}
如果您使用
Dim myFileDlog As New OpenFileDialog()
然后您可以使用它来还原最后一个目录
myFileDlog.RestoreDirectory = True
这是不是
myFileDlog.RestoreDirectory = False
(在VB.NET中)我知道这是一个有点老掉牙的问题,但我无法找到我喜欢的解决方案,所以我开发了自己的解决方案。我在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));
}
}
我个人喜欢这种方法,因为我不会在多个表单中重复使用同一个文件选择器实例,也不会设置InitialDirectory属性。。。。但我想你的意思是:OpenFile.restoreditory=true;OpenFile.RestoreDirectory=true和OpenFile.RestoreDirectory=false,两者都可以工作,但不设置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));
}
}