none
Files Synced to One Drive is not opened as Read-Only RRS feed

  • Question

  • We are facing the exception (The process cannot access the file 'C:\OneDrive\input.xlsx' because it is being used by another process) while opening the Excel file in read-only mode using file stream, when the file is synced with One Drive and it's already opened in Microsoft Excel.
     
    Code Snippet:
    FileStream stream = new FileStream(@"C:\OneDrive\input.xlsx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

    Note: The file is properly opened when it is not synced with One Drive.
     
    Please share us the solution to open the One Drive synced Excel file in read-only mode using FileStream when it's already opened in Microsoft Excel.


    Monday, May 6, 2019 10:31 AM

All replies

  • This is by design. Office is OneDrive and SharePoint aware. If you open files from either of these locations it has extra hooks to ensure things stay in sync. I'm not aware of any way to work around it. Consider posting this in the Office forums and maybe somebody can tell you how to disable some obscure setting.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, May 6, 2019 2:11 PM
    Moderator
  • Hi Abirami,

    Thank you for posting here.

    For your question, you want to open the OneDrive synced Excel file in read-only mode using FileStream when it's already opened in Microsoft Excel.

    Based on my test, I could not reproduce your problem according to your code. I think your code is correct for your question.

    Code:

      FileStream s2 = new FileStream(@"C:\Users\username\OneDrive\test.xlsx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                Console.WriteLine("success");
                Console.ReadKey();

    Test Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 7, 2019 2:26 AM
    Moderator
  • FYI, the issue is with referencing the document from One Drive specifically, not your local user account. If the document is open directly from One Drive in Excel 365 with or without the auto save and other options set AND you attempt to open the same file using a FileStream you get the error. The share permissions (in Windows) are correct in the sense that Excel has opened the file with RWD share privileges but you still get the error. This doesn't happen if you don't have the file open in Excel, from One Drive. I replicated with the latest version of Windows 10 running the latest version of Excel 365.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, May 7, 2019 2:55 AM
    Moderator
  • Hi Jack,

     If the document uploaded in One Drive account is opened in Excel after downloading to local path and attempting to open the same file using a FileStream the error is occured. Please share if there any possibility to overcome this error.

    Regards,

    Abirami


    Monday, January 13, 2020 12:51 PM