tags:

views:

71

answers:

1

Is there a way to learn the username of the user that has a file open?

I am developing a program that will be a desktop client for many users. The application will open some files and I'd like to allow many users to open the files at the same time, but only allow the first user to have write privileges. What I want is to be able to tell the other users who has write access to a file.

Is that something that can be learned by an application? (I am developing in VS 2008).

+2  A: 

Windows doesn't support it. But you easily add it yourself. When you gain write access to the file, write another hidden file with the user name (Environment.UserName) in the same directory (like ~originalname.username).

When you fail to gain write access, try to open that file to read the user name. Sleep for a second if that fails.

Hans Passant
That is a valid solution. But I think you can really do that with the Windows API. You can learn what processes have a file open, and you can learn what user owns a process. I don't know the right API calls, but it seems possible.
Martinho Fernandes
NtQuerySystemInformation, the actual arguments are undocumented and structures are different for each operating system. More to the point: the process that owns the file may well exist on some machine thousands of miles away, connected through a VPN tunnel. Well out of reach from your machine.
Hans Passant