c#在winforms中使用openFileDialog从繁忙的文件中获取路径

c# Get Path from a busy file using openFileDialog in winforms

我正在尝试使用 winforms 和 C# 中的 openfiledialog 获取 'busy' 文件的路径。使用以下代码:

 private void button1_Click(object sender, EventArgs e)
    {

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

当我指向有问题的文件时,出现以下错误:

错误信息:

MyDbContext.mdf

This file is in use.

Enter a new name or close the file that's open in another program.

这很好,因为我已经知道该文件正在使用中,我只想将文件路径存储到一个字符串中,而不打开它。

也许这里openfiledalog是错误的选项,毕竟我不想打开文件,只是为了列出它的路径。但是我没有在 winforms 中找到指向文件的任何其他内容。

有没有其他方法可以达到我想要的效果?

尝试在 ShowDialog() 之前将 ValidateNames 设置为 false

openFileDialog1.ValidateNames = false;