Search code examples
c#winformsopenfiledialog

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


I'm trying go get a path to a 'busy' file using openfiledialog in winforms and C#. Using the following code:

 private void button1_Click(object sender, EventArgs e)
    {

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

When I point to the file in question I get the following error:

Error Message:

MyDbContext.mdf

This file is in use.

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

Which is fine, because I already know the file is in use, all I want is to store the file path into a string, without opening it.

Perhaps openfiledalog is the wrong option here, after all I don't want to open the file, only to list it's path. However I didn't find anything else in winforms that points to a file.

Is there any other means to achieve what I want?


Solution

  • Try setting ValidateNames to false before ShowDialog()

    openFileDialog1.ValidateNames = false;