Find out who locked the file on a network drive in C #

The program runs on several computers that share a network drive. He can use

... = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read); 

to lock the file from writing. Then all other instances can only read and display a warning that the file is not writable.

How do I find out who (that is, which machine) locked a file to display this warning?

+6
source share
2 answers

The only way I've ever seen this is with a program that opens a file to leave a marker file (.lock) or similar. This .lock file can obviously contain everything you need (username, machine, etc.) and can be read separately.

This assumes that you have control over the software that reads it on another PC.

+3
source

Here is a C # source code message for an example of how to view a list of processes and check files locked by each process.

How to find out which process blocked a file using C # .

The next step is to use this functionality in the service on each machine, so that the process can send a request for a specific file name, and then get an answer about whether the process is blocked on this computer.

The data may include the process name, user ID, and other information available from the process list.

This approach works more, but what it does is to provide a way to access information without requiring file-blocking applications to do something special.

On the other hand, if the files that interest you are under your control, and you can determine access to files, this is probably too large.

+1
source

Source: https://habr.com/ru/post/922944/


All Articles