Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine who has a file open using C#

Tags:

c#

.net

file

Using C# how can I get information about who has a file open? User name and machine name would be sufficient.

In case it matters I have Windows workstations accessing files on a Linux file server via Samba. I need this information in a program running on the workstations.

like image 990
mohnston Avatar asked May 04 '11 17:05

mohnston


People also ask

How do you check if a file has been opened in C?

Open the file using the "fopen" function and assign the "file" to the variable. Check to make sure the file was successfully opened by checking to see if the variable == NULL. If it does, an error has occured.

What opens file in C?

The fopen() method in C is a library function that is used to open a file to perform various operations which include reading, writing etc.

How do you check a file is present or not in C?

Using fopen() function You can use fopen() function to open given file in read mode. If it returns NULL then file does not exists otherwise exists on disk. Testing file existence using fopen() is not reliable. fopen() fails if you don't have read/write/execute permissions on file.


1 Answers

The core .NET libraries do not have any means to do this.

And if I understand you correctly, you want to know from Windows workstation A who has files open on the Linux file share and some of those users with open file might be origination from other windows boxes, if that is the case then you will need to have a service on the Linux side which you can query to provide that back to your windows work station.

On the local machine this can be achieved, but at the very least your will need to interop to OS APIs like NtQueryInformationFile and NtQueryObject (both not officially documented) amongst others. Tools like process monitor dynamically install a device driver to achieve the level of inspection that they do and that will only tell you which local file handles are open by which user.

like image 165
Chris Taylor Avatar answered Sep 25 '22 19:09

Chris Taylor