Issue opening file using WinForms application (Open file dialog) where file location is OneDrive

Eswar Chaitanya Darsi 1 Reputation point
2021-10-12T10:52:54.273+00:00

Hi,

I am seeing an issue with File Open Dialog when I try to open a file from OneDrive location mapped on my machine.

Issue / Steps to reproduce:

  1. Copy any file (ex: excel file) to OneDrive
  2. Double click and open the file from OneDrive location to edit
  3. From any windows forms application (.NET Framework 4.8), when I try to open the same file using File dialog window I get an error message "This file is in use. Enter a new name or close the file that's open in another program" public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    openFileDialog1 = new OpenFileDialog();
    }
         private void button1_Click(object sender, EventArgs e)  
         {  
             openFileDialog1.InitialDirectory = "C:\\";  
             var result = openFileDialog1.ShowDialog();  
             if(result == DialogResult.OK)  
             textBox1.Text = openFileDialog1.FileName;  
         }  
     }  
    

139829-capture.png

This issue is not reproducible when I open the file from any local/network shared directory.

Developer technologies | Windows Forms
{count} votes

1 answer

Sort by: Most helpful
  1. Eric Brenner 0 Reputation points
    2026-01-21T14:18:21.63+00:00

    If anyone else comes across this, set the ValidateNames property to False before showing the OpenFileDialog dialog box. This should get you around the "file is in use" Warning dialog.

    https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.filedialog.validatenames?view=windowsdesktop-10.0

    I was using a different programming tool - National Instruments LabVIEW - and received the same message. The forum members thre led me to this resolution.

    There must be something happening with how Windows handles Office files saved to OneDrive and how it handles them when someone requests to open them. We could not find any documentation on that.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.