Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I simply 'read' a file that is in use?

I am trying to use a StreamReader to read a file, but it is always in use by another process so I get this error:

The process cannot access the file '\arfjwknasgmed17\C$\FLAG CONDITION\CP-ARFJN-FLAG.XLS' because it is being used by another process.

Is there a way I can read this without copying it? Or is that my only option?

like image 345
naspinski Avatar asked Oct 15 '08 06:10

naspinski


People also ask

Is there a way to check if a file is in use?

The common managed way to check whether a file is in use is to open the file in a try block. If the file is in use, it will throw an IOException. Another way to check whether a file is in use is to call the CreateFile API. If a file is in use, the handle return is invalid.

How check file is open or not in C#?

try { using (Stream stream = new FileStream("MyFilename. txt", FileMode. Open)) { // File/Stream manipulating code here } } catch { //check here why it failed and ask user to retry if the file is in use. }

How do you check if a file is being used by another process in VB net?

To check to see if the file is in use, I move it to another file and catch an error if it is in use. If no problems occurr I rename it back. At least that was my plan... This program is used over the network without any services running.

Can you read and write to a file at the same time?

It is possible to read and write the same file at different times, and it is possible to read one file and write another at the same time, but it isn't possible to read and write the same file at the same time.


1 Answers

You can read the file only if the program that opened the file first specified read sharing rights on that file.

If the file does indeed have no read sharing rights though, you wouldn't be able to copy it in the first place.

You may not be able to access a file if you are specifying a sharing right that conflicts with the sharing right of a program that already has the file opened. For example you can't grant write access if the program that already has it opened isn't granting write access.

If the program that opened the file in the first place supports Volume Shadow Copy (VSS), you can also use VSS to gain access to the file.

There are commercial software drivers that allow you to access such files, even when they are in use. You used to be able to get Open File Manager by St-Bernards, and you can also use File Access Manager (FAM) by VisionWorks Solutions Inc. These drivers are typically OEM'ed to backup software companies for inclusion in their products.

VSS works by telling the program that has the file opened already that another program would like to read from the file. VSS then does a copy of the file and lets you read from this copy. VSS does not work for legacy applications.

FAM transparently works for legacy and non-legacy programs alike by specifying an 'allowed list' of applications that can access exclusively opened and locked files. Only programs in this list are allowed access to these files. When a file is being opened, it goes into cache mode so that you will obtain a copy of the file as it was when the 'backup/open' of the file started. At this point the program that originally opened the file sees the file as it actually is, and the second program in the allowed list, sees the file as it was when the 'open/backup' of the file happened. This ensures consistency of the file.

like image 196
Brian R. Bondy Avatar answered Sep 19 '22 14:09

Brian R. Bondy