6

A program is running on multiple machines that share a network drive. It can use

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

to lock a file from writing. All the other instance then can only read it and display a warning, that the file is not writable.

How can I find out who (i.e. which machine) locked the file, to display that along the warning?

Niklas
  • 3,753
  • 4
  • 21
  • 29

2 Answers2

3

The only way I have ever seen this achieved is for the program that opens the file to leave behind a marker file, (.lock) or similar. This .lock file can then obviously contain whatever you want (username, machine etc) and can be read separately.

This assumes you have control over the software which is reading it on the other PC.

KingCronus
  • 4,509
  • 1
  • 24
  • 49
1

Here is a posting with C# source code for an example of how to look through the process list and check the files that are locked by each process.

How does one figure out what process locked a file using C#.

The next step would be to use this functionality within a service on each machine so that a process can send a query for a specific file name and then receive a response as to whether a process on that machine has it locked.

The data could include process name, user id, and other information available from the process list.

This approach is more work however what it does is provide a way to access the information without require applications locking the file to do something special.

On the other hand if the files you are interested in are within your control and you can determine the file access, this is probably overkill.

Community
  • 1
  • 1
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
  • I am wondering the same thing. From what I can tell, most of the lock detection relies on being able to look through the process list and to then look at which files the various processes running have open and/or locked. One approach would be to have a service running on each machine that would provide a kind of SNMP or similar type of query server to allow other processes to query file open/lock status. – Richard Chambers Aug 15 '12 at 14:34
  • Intresting idea. I probably would have to do that, if I had no control over the programs that could lock the file. Since I have, lock files sound easier. – Niklas Aug 16 '12 at 08:11