Search code examples
c#winformsopenfiledialog

Open File Which is already open in window explorer


I am trying to open a file using OpenFileDialog.

if (openFileDialog1.FileName != "" && resultSaveDialog == System.Windows.Forms.DialogResult.OK)
        {
            openFileDialog1.OpenFile(); // Throw Exception Here
            txtFileName.Text = openFileDialog1.SafeFileName;                
        }

But if file is already opened in window explored it throws me following exception

The process cannot access the file 'D:\Projects\CDR_RAW_FILES\GroupData\8859511378.xls' because it is being used by another process.

Is it possible to open the file Using OpenFileDialog even if file has already opened in window Explorer.


Solution

  • Ok, If you just need the Selected File name and its path, Then try like below, It will help you...

    if (openFileDialog1.FileName != "" && resultSaveDialog == System.Windows.Forms.DialogResult.OK)
    {
     string path = Path.GetDirectoryName(openFileDialog1.FileName);
     string filename = Path.GetFileName(openFileDialog1.FileName);
     txtFileName.Text = filename;
    }