简体   繁体   English

在 C# 中存储目录路径

[英]Store directory path in C#

I have to make a folder path reusable.我必须使文件夹路径可重用。 When I open a folder the program must save it's directory one time so I can instantly reopen it when clicking the button without navigation in the folders.当我打开一个文件夹时,程序必须一次保存它的目录,这样我就可以在单击按钮时立即重新打开它,而无需在文件夹中导航。 I thought I create a string and store the path directory in it for a time.我以为我创建了一个字符串并将路径目录存储在其中一段时间​​。

How can I make this work?我怎样才能使这项工作? I store the filepath in a textbox for now:我现在将文件路径存储在文本框中:

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;
        }

You could use a static string variable:您可以使用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);
    }
}

I think what you need is for a way your form to remember for example the last used path.我认为您需要的是让您的表单记住例如上次使用的路径。 The official way of doing this to go to project properties and then into settings and add a new setting value of type string to store path or filename information.这样做的官方方法是转到项目属性然后进入设置并添加一个新的字符串类型的设置值来存储路径或文件名信息。

图。1

Then you need two more things.那么你还需要两件事。

  • Bind a textbox to the property for display purposes if needed.如果需要,将文本框绑定到属性以用于显示目的。 You do this manually in a .NET Core application by adding the following code into the Form1.Designer.cs file您可以在.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)); ... }

    Or if you are with .NET Framework then there is a UI option for the textbox under ApplicationSettings .或者,如果您使用.NET FrameworkApplicationSettings下的文本框有一个 UI 选项。 图2

  • The last item you need for the form to save the settings when it is closing.表单关闭时保存设置所需的最后一项。 This is done by handling the FormClosing event这是通过处理FormClosing事件来完成的

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

Now your code can read and write to the textbox what it needs, and the settings file is going to kelp synchronized.现在您的代码可以读取和写入它需要的文本框,并且设置文件将同步进行。 For example in file opening operation, if you wish to store last lath in the textbox then do the following:例如在文件打开操作中,如果您希望在文本框中存储最后一个板条,请执行以下操作:

    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);
        }

This way each time the application is launched it remembers the contents of the text box这样每次启动应用程序时它都会记住文本框的内容

图3


Note that the application will create a settings XML file in the following location:请注意,该应用程序将在以下位置创建一个设置 XML 文件:

文件

You can use Path.GetFullPath您可以使用Path.GetFullPath

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

You could store the path in a variable using what Jamiec said您可以使用 Jamiec 所说的将路径存储在变量中

then you would open the file a second time using system.io.open()然后您将使用 system.io.open() 第二次打开文件

link to the documentation 文档链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM