3

I am trying to get last modified by user name but I am not able to get the user name. I searched lot on Google but I am confuse now. Because In some thread I found that last modified user is not able to get. And Other link gives some code. I used that code but last modified user is not getting retrieved.

Is there any one who get the last modified user name using c# code?
Is it possible?

  • 1
    string user = System.IO.File.GetAccessControl("filepath").GetOwner(typeof(System.Security.Principal.NTAccount)).ToString(); this code retrieve file owner – Raju Joshi Feb 25 '14 at 12:31
  • @Grant Winney I have tried the code of the link which is provided by HABJAN. –  Feb 25 '14 at 13:16

1 Answers1

3

This should give you the last modified file and the user who changed it:

var dir = new DirectoryInfo(path);
var lastModified = dir.GetFiles()
    .OrderByDescending(fi => fi.LastWriteTime)
    .First();
string modifiedBy = lastModified.GetAccessControl()
    .GetOwner(typeof(System.Security.Principal.NTAccount)).ToString();

"Powered by": Finding the user who modified the shared drive folder files

Edit: actually this gives you only the owner of the file that was last modified.

It seems that windows simply doesn't keep track of this. You could use a FilesystemWatcher to do it yourself.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • this function return user who create this file – Raju Joshi Feb 25 '14 at 12:36
  • @RajuJoshi: which function? `LastWriteTime` is the time a file was last written to. – Tim Schmelter Feb 25 '14 at 12:39
  • 2
    string modifiedBy = lastModified.GetAccessControl() .GetOwner(typeof(System.Security.Principal.NTAccount)).ToString(); this line return file owner, not a modifier – Raju Joshi Feb 25 '14 at 12:42
  • 1
    @RajuJoshi: you're right. I'm afraid that windows doesn't store this so you need to keep track of it yourself. One way, use a `FileSystemWatcher` (edited my answer acc.). – Tim Schmelter Feb 25 '14 at 13:04
  • string modifiedBy = lastModified.GetAccessControl() .GetOwner(typeof(System.Security.Principal.NTAccount)).ToString(); its also not working if user is from another domain – Raju Joshi Feb 25 '14 at 13:22