FileSystemWatcher - How to get the user who has modified C # file

0

I have developed an application that monitors events in the filesystem using the FileSystemWatcher class and would like to leave it installed on the file server as it happens that I need to register the user who accessed the file over the network.

Note: locally I know I can use:

System.Security.Principal.WindowsIdentity.GetCurrent().Name

or

ManagementObjectSearcher searcher = 
                 new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem");
ManagementObjectCollection collection = searcher.Get();

and the network?

    
asked by anonymous 10.01.2018 / 21:43

1 answer

0

In the title you say user that changed, but in the question you say the user who accessed.

If you want the user you have accessed, you will not be able to see the properties of the file. You'll probably have to implement file access control.

If you want the last user you changed, you will have difficulties when you are from another domain.

If you are in the same domain, try the following code:


var usr = System.IO.File.GetAccessControl(path).GetOwner(typeof(System.Security.Principal.SecurityIdentifier));

or


var usr = System.IO.File.GetAccessControl(path).GetOwner(typeof(System.Security.Principal.NTAccount));
    
11.01.2018 / 20:37