簡體   English   中英

在 C# 中存儲目錄路徑

[英]Store directory path in C#

我必須使文件夾路徑可重用。 當我打開一個文件夾時,程序必須一次保存它的目錄,這樣我就可以在單擊按鈕時立即重新打開它,而無需在文件夾中導航。 我以為我創建了一個字符串並將路徑目錄存儲在其中一段時間​​。

我怎樣才能使這項工作? 我現在將文件路徑存儲在文本框中:

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

您可以使用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);
    }
}

我認為您需要的是讓您的表單記住例如上次使用的路徑。 這樣做的官方方法是轉到項目屬性然后進入設置並添加一個新的字符串類型的設置值來存儲路徑或文件名信息。

圖。1

那么你還需要兩件事。

  • 如果需要,將文本框綁定到屬性以用於顯示目的。 您可以在.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 FrameworkApplicationSettings下的文本框有一個 UI 選項。 圖2

  • 表單關閉時保存設置所需的最后一項。 這是通過處理FormClosing事件來完成的

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

現在您的代碼可以讀取和寫入它需要的文本框,並且設置文件將同步進行。 例如在文件打開操作中,如果您希望在文本框中存儲最后一個板條,請執行以下操作:

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

這樣每次啟動應用程序時它都會記住文本框的內容

圖3


請注意,該應用程序將在以下位置創建一個設置 XML 文件:

文件

您可以使用Path.GetFullPath

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

您可以使用 Jamiec 所說的將路徑存儲在變量中

然后您將使用 system.io.open() 第二次打開文件

文檔鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM