【问题标题】:Store directory path in C#在 C# 中存储目录路径
【发布时间】:2020-11-09 12:41:24
【问题描述】:

我必须使文件夹路径可重用。当我打开一个文件夹时,程序必须保存它的目录一次,这样我就可以在单击按钮时立即重新打开它,而无需在文件夹中导航。 我以为我创建了一个字符串并将路径目录存储在其中一段时间​​。

我怎样才能做到这一点? 我现在将文件路径存储在文本框中:

OpenFileDialog openFileDialog1 = new OpenFileDialog
        {
            InitialDirectory = @"D:\",
            Title = "Browse Text Files",

            CheckFileExists = true,
            CheckPathExists = true,

            DefaultExt = "txt",
            Filter = "txt files (*.txt)|*.txt",
            FilterIndex = 2,
            RestoreDirectory = true,

            ReadOnlyChecked = true,
            ShowReadOnly = true
        };

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            textBox1.Text = openFileDialog1.FileName;
        }

【问题讨论】:

  • 是的,但是文件夹路径可能会改变,取决于你打开什么样的 txt/xml
  • 当您使用 RestoreDirectory = false 时,它​​已经以这种方式工作了。或者不分配 InitialDirectory。

标签: c# openfiledialog savefiledialog


【解决方案1】:

您可以使用static string 变量:

private const string initDefaultPath = @"D:\"; // <-- initial default folder path
private static string prevFolderPath = initDefaultPath;

//Let's suppose a button click event
public void Button1_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog1 = new OpenFileDialog
    {
        InitialDirectory = string.IsNullOrWhiteSpace(prevFolderPath)
            ? initDefaultPath
            : prevFolderPath,

        Title = "Browse Text Files",
        CheckFileExists = true,
        CheckPathExists = true,
        DefaultExt = "txt",
        Filter = "txt files (*.txt)|*.txt",
        FilterIndex = 2,
        RestoreDirectory = true,
        ReadOnlyChecked = true,
        ShowReadOnly = true
    };

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        textBox1.Text = openFileDialog1.FileName;
        prevFolderPath = Path.GetDirectoryName(openFileDialog1.FileName);
    }
}

【讨论】:

  • 所以当我想打开同一个保存的目录时,我应该参考 prevFolderpath 吗?
  • 没错,这可能是一个解决方案。注意始终检查它是否为空,否则会抛出 NullReferenceException
  • 我已经编辑了,添加了一个const string,您可以在其中存储您的初始默认路径,以便您可以在一个地方进行更改(如果需要)。
  • 此解决方案无法在应用程序关闭并重新启动后继续存在。使用静态可变变量,您创建了一个违反 C# 的许多设计原则的全局变量。
  • 我只需要在应用程序运行时记住它。我现在做到了,它有效。非常感谢!
【解决方案2】:

我认为您需要一种方式来记住您的表单,例如上次使用的路径。这样做的官方方式是进入项目属性,然后进入设置,并添加一个字符串类型的新设置值来存储路径或文件名信息。

那么你还需要两件事。

  • 如果需要,将文本框绑定到属性以进行显示。您可以在.NET Core 应用程序中手动执行此操作,方法是将以下代码添加到Form1.Designer.cs 文件中

    private void InitializeComponent()
    {
        ...
        this.textBox1.DataBindings.Add(
            new System.Windows.Forms.Binding("Text", 
            global::WindowsFormsApp1.Properties.Settings.Default, 
            "lastPath", 
            true, 
            System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
        ...
    }
    

    或者,如果您使用的是.NET Framework,那么 ApplicationSettings 下的文本框有一个 UI 选项。

  • 表单关闭时保存设置所需的最后一项。这是通过处理FormClosing 事件来完成的

     private void Form1_FormClosing(object sender, FormClosingEventArgs e)
     {
         Properties.Settings.Default.Save();
     }
    

现在您的代码可以读取和写入文本框所需的内容,并且设置文件将同步进行 kelp。例如在文件打开操作中,如果您希望将最后一个板条存储在文本框中,请执行以下操作:

    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog
        {
            InitialDirectory = textBox1.Text,
            Title = "Browse Text Files",

            CheckFileExists = true,
            CheckPathExists = true,

            DefaultExt = "txt",
            Filter = "txt files (*.txt)|*.txt",
            FilterIndex = 2,
            RestoreDirectory = true,

            ReadOnlyChecked = true,
            ShowReadOnly = true
        };

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            textBox1.Text = System.IO.Path.GetDirectoryName(openFileDialog1.FileName);
        }

这样每次启动应用程序时它都会记住文本框的内容


请注意,应用程序将在以下位置创建一个设置 XML 文件:

【讨论】:

    【解决方案3】:

    您可以使用Path.GetFullPath

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        textBox1.Text = Path.GetFullPath(openFileDialog1.FileName);
    }
    

    【讨论】:

      【解决方案4】:

      您可以使用 Jamiec 所说的将路径存储在变量中

      然后您将使用 system.io.open() 再次打开该文件

      link to the documentation

      【讨论】:

      • 我会看看这个
      • system.io.open() 绝对不是 .NET 框架/核心的东西。你是在说System.IO.File.Open() 吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-12
      相关资源
      最近更新 更多