简体   繁体   中英

Opening excel file locked by OneDrive

I need to read from an excel file while it is open in excel, and the file is stored in a synced OneDrive folder.

The following works flawlessly when the file is open in excel and OneDrive syncing is paused, or the file is in a non OneDrive folder:

var fileStream = new FileStream(zipPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)

However, when the file is in OneDrive and OneDrive is syncing, I get the following exception:

System.IO.IOException: The process cannot access the file 'C:\Users\xxxx' because it is being used by another process.

This exception would be expected if a stricter FileShare, FileAccess or FileMode was used, but in this case it seems that it is actually OneDrive that locks the file in a specific way.

Does anybody have experience with and/or a solution to this?

You could copy that file to new location and open it from there.

var sourceFile = new FileInfo("C:\Users\xxxx");
sourceFile.CopyTo("C:\Users\AnotherLocationNotBeingSyncedWIthOneDrive\xxxx2", true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM