2

I have a shared drive which is elsewhere on a server. I want to get a notification which gives me the user name of the person who has modified any file present in the shared drive.

Currently I am using the FileSystemWatcher to get the notification and the code provided by Stack overflow question "Find out username(who) modified file in C#" to find the user name.

But Instead I get the name of the computer on which the shared drive is at the moment. I want the username who had modified the file on the shared drive.

My piece of code is :

 private string GetSpecificFileProperties(string file, params int[] indexes)
        {
            string fileName = Path.GetFileName(file);
            string folderName = Path.GetDirectoryName(file);
            Shell32.Shell shell = new Shell32.Shell();
            Shell32.Folder objFolder;
            objFolder = shell.NameSpace(folderName);
            StringBuilder sb = new StringBuilder();
            foreach (Shell32.FolderItem2 item in objFolder.Items())
            {
                if (fileName == item.Name)
                {
                    for (int i = 0; i < indexes.Length; i++)
                    {
                        sb.Append(objFolder.GetDetailsOf(item, indexes[i]) + ",");
                    }
                    break;
                }
            }
            string result = sb.ToString().Trim();
            if (result.Length == 0)
            {
                return string.Empty;
            }
            return result.Substring(0, result.Length - 1);
        }




string Type = GetSpecificFileProperties(filePath, 2);
string ObjectKind = GetSpecificFileProperties(filePath, 11);
DateTime CreatedDate = Convert.ToDateTime(GetSpecificFileProperties(filePath, 4));
DateTime LastModifiedDate = Convert.ToDateTime(GetSpecificFileProperties(filePath, 3));
DateTime LastAccessDate = Convert.ToDateTime(GetSpecificFileProperties(filePath, 5));
string LastUser = GetSpecificFileProperties(filePath, 10);
string ComputerName = GetSpecificFileProperties(filePath, 53);
string FileSize = GetSpecificFileProperties(filePath, 1);
Community
  • 1
  • 1
Ram Mehta
  • 449
  • 1
  • 6
  • 20
  • It's not always possible to know who changed the file because if you share the drive with possibility of anonymous access, any connection will not be authenticated and the server just won't know who accessed the disk remotely. – Eugene Mayevski 'Callback Nov 10 '13 at 13:27
  • @EugeneMayevski'EldoSCorp The users who would be accessing the file are the ones who have the permission. So wont that be possible to get the User Name of the person who is accessing or creating the file ? I guess in Properties of the file, in Security tab we have the Users name given in the list. – Ram Mehta Nov 11 '13 at 11:41
  • If you have anonymous access disabled and users authenticate to access the shared disk, then the filesystem knows the user name that accessed the file. You can obtain it if the filesystem driver is yours (which is rarely the case) or when you use filesystem filter driver to intercept file writing requests. – Eugene Mayevski 'Callback Nov 11 '13 at 11:48
  • @EugeneMayevski'EldoSCorp Can you give me some rough idea of how to use the FileSystem Filter Driver ?? – Ram Mehta Nov 14 '13 at 12:06
  • This is too broad for SO comment. The filter driver has access to information about the user accessing the data. – Eugene Mayevski 'Callback Nov 14 '13 at 14:36
  • You are reinventing file access auditing, which is built into Windows. Instead of detecting the access yourself, why not design your application to present the audit logs in a more friendly way? – Ben Voigt Nov 18 '13 at 20:46
  • @BenVoigt Thanks, at the moment I am trying to use the FileSecurity Class to get the owner and the group of users associated with the users. – Ram Mehta Nov 26 '13 at 08:21
  • @EugeneMayevski'EldoSCorp Thanks for the help, I have posted the answer which I have used. – Ram Mehta Nov 26 '13 at 19:37

1 Answers1

2

I have got the fix to that, It can be achieved using the ObjectSecurity Class of .NET. In that we can use the GetOwner. It fetches the owner of the file who has modified / created a file. This is the piece of code which would help:

string owner = System.IO.File.GetAccessControl(e.FullPath).GetOwner(typeof(System.Security.Principal.NTAccount)).ToString();
Console.WriteLine(owner);
Ram Mehta
  • 449
  • 1
  • 6
  • 20
  • 2
    Don't understand why this is marked as accepted answer. Your code returns the owner, not the user who last modified the file. – anhtv13 Aug 15 '19 at 07:49