15

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.

mohnston
  • 737
  • 1
  • 6
  • 18

2 Answers2

4

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.

Chris Taylor
  • 52,623
  • 10
  • 78
  • 89
  • You understood my question correctly. I thought there might be such an animal in System.IO. I was probably thinking of the AutoCAD API which has a WhoHasInfo method that gets info about a .DWG (or related) file. It gets user name, computer name, isFileLocked, and OpenTime. – mohnston May 04 '11 at 18:39
  • What if the remote file is on Windows Server exposed through a shared folder? Is it SAMBA that is the issue in this case, or is it an actual deficiency in the .NET Libraries. – Doug Wilson Jan 25 '12 at 23:23
  • To help others: Since it is a Linux Samba file server, then you'll need to exec the "smbstatus" command, capture and parse the output. which includes: user, machine, and files. You can even request a list of only locked files with the -L option (I'm trying to do the same in Java and this is the only path I've found), although I would _really_ like a solution that works for a windows 2008 file server via linux there isn't a solution that is as obvious. – Quaternion Mar 27 '14 at 23:38
1

I don't think you can do this using .NET

In the past when I've run into this issue, I've always ended up using the process monitor or process explorer.

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486