C# 查找修改共享驱动器文件夹文件的用户

C# 查找修改共享驱动器文件夹文件的用户,c#,filesystemwatcher,C#,Filesystemwatcher,我有一个共享驱动器,它位于服务器的其他位置。我想得到一个通知,告诉我修改共享驱动器中任何文件的人的用户名 目前,我正在使用FileSystemWatcher获取堆栈溢出问题“”提供的通知和代码,以查找用户名 但我得到的是目前共享驱动器所在的计算机的名称。我想要在共享驱动器上修改文件的用户名。 我的代码是: private string GetSpecificFileProperties(string file, params int[] indexes) {

我有一个共享驱动器,它位于服务器的其他位置。我想得到一个通知,告诉我修改共享驱动器中任何文件的人的用户名

目前,我正在使用FileSystemWatcher获取堆栈溢出问题“”提供的通知和代码,以查找用户名

但我得到的是目前共享驱动器所在的计算机的名称。我想要在共享驱动器上修改文件的用户名

我的代码是:

 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);
私有字符串GetSpecificFileProperties(字符串文件,参数int[]索引)
{
字符串文件名=Path.GetFileName(文件);
字符串folderName=Path.GetDirectoryName(文件);
Shell=new Shell32.Shell();
Shell32.文件夹objFolder;
objFolder=shell.NameSpace(folderName);
StringBuilder sb=新的StringBuilder();
foreach(objFolder.Items()中的Shell32.FolderItem2项)
{
if(fileName==item.Name)
{
for(int i=0;i
我已经解决了这个问题, 它可以使用.NET的ObjectSecurity类实现。因为我们可以使用GetOwner。 它获取修改/创建文件的文件所有者。 这段代码将有助于:

string owner = System.IO.File.GetAccessControl(e.FullPath).GetOwner(typeof(System.Security.Principal.NTAccount)).ToString();
Console.WriteLine(owner);

不可能总是知道是谁更改了文件,因为如果您共享驱动器时可能会进行匿名访问,则任何连接都不会经过身份验证,服务器也不会知道是谁远程访问了磁盘。@EugeneMayevski'EldoSCorp访问文件的用户是有权限的用户。那么,难道不可能获得访问或创建文件的人的用户名吗?我猜在文件的属性中,在“安全”选项卡中,我们有列表中给定的用户名。如果您禁用了匿名访问,并且用户通过身份验证访问共享磁盘,则文件系统知道访问该文件的用户名。如果文件系统驱动程序是您的(这种情况很少发生),或者当您使用文件系统筛选器驱动程序拦截文件写入请求时,您可以获得它。@EugeneMayevski'EldoSCorp您能给我一些关于如何使用文件系统筛选器驱动程序的粗略想法吗?这太宽泛了,无法发表评论。筛选器驱动程序可以访问有关访问数据的用户的信息。不理解为什么将此标记为已接受答案。代码返回的是所有者,而不是上次修改文件的用户。