0

I have a desktop application and below is the flow that is to be followed. During app initialization, an API should be hit and an excel should downloaded to a shared location. After download is complete the app should read the excel file. This won't be a problem with a single instance of app running. But since this is a desktop app, multiple instances (on different computers) are run, app every time during initialization, downloads the file. I'm using OLE Db engine to read the file and the file is being locked and there 's error "The ole db engine cannot read the file because it is opened by another user " while another instance of the app is opened. How to prevent this?

if (response.Result.IsSuccessStatusCode)
                    {
                        using (Stream streamToWriteTo = new FileStream(pathToDownloadReport, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        {
                            response.Result.Content.CopyToAsync(streamToWriteTo).Wait();
                        }
                    }
  • Excel itself locks the file ? Check if the file is already and the position and do not download it? On start of download: create a lock-file and pause in all other apps if that lockfile is present until it is deleted again and the excel is there? LOTs of ways .... beside that, if you read the excel yourself specify it can be read/write shared when opening it – Patrick Artner Jul 24 '20 at 07:39
  • I'm using OLE Db engine to read the file and yes it's being locked – Toruk makto Jul 24 '20 at 07:41
  • 1
    Does this answer your question? [Accessing a shared file?](https://stackoverflow.com/questions/26421683/accessing-a-shared-file) – Patrick Artner Jul 24 '20 at 07:42
  • Have you checked [all of those](https://www.google.com/search?q=The+ole+db+engine+cannot+read+the+file+because+it+is+opened+by+another+user+site:stackoverflow.com)? Without seeing sources and understand what exactly you doing it's kind of hard to find for you duplicate. – Sinatr Jul 24 '20 at 07:45
  • To be precise , i'm getting this error "The Microsoft Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data." – Toruk makto Jul 24 '20 at 08:03
  • I followed the above link for solution and it doesn't work for me – Toruk makto Jul 24 '20 at 08:05
  • Here is the code ` if (response.Result.IsSuccessStatusCode) { using (Stream streamToWriteTo = new FileStream(pathToDownloadReport, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { response.Result.Content.CopyToAsync(streamToWriteTo).Wait(); } }` – Toruk makto Jul 24 '20 at 08:06
  • If another application has already opened the file and not shared it, then nothing can be done. Only periodically repeat attempts to access it. – Alexander Petrov Jul 24 '20 at 08:25

1 Answers1

0

If you want to have concurrent access to a file you need to make sure every client only takes a read-lock on the file. With OleDb you can open a connection to the file with ReadOnly access with a connection.

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\path\to\your\exelfile.xlsx;Extended Properties=\"Excel 12.0;IMEX=1;ReadOnly=true;\""

You still need to make sure no one opens the file in Excel.

Since you only can have read-only access to your file, you might as well make a local copy of the file and open that instead. That way locking won't be a problem.

JonC
  • 978
  • 2
  • 7
  • 28