2

サーバー上の別の場所に共有ドライブがあります。共有ドライブに存在するファイルを変更した人のユーザー名を知らせる通知を受け取りたいです。

現在、私は FileSystemWatcher を使用して通知を取得し、スタック オーバーフローの質問 " Find out username(who) modified file in C# " によって提供されるコードを使用して、ユーザー名を見つけています。

しかし、代わりに、共有ドライブが現在あるコンピューターの名前を取得します。共有ドライブ上のファイルを変更したユーザー名が必要です。

私のコードは次のとおりです。

 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);
4

1 に答える 1

2

私はそれを修正しました。.NET の ObjectSecurity クラスを使用して実現できます。その中で、GetOwner を使用できます。ファイルを変更/作成したファイルの所有者を取得します。これは役立つコードです:

string owner = System.IO.File.GetAccessControl(e.FullPath).GetOwner(typeof(System.Security.Principal.NTAccount)).ToString();
Console.WriteLine(owner);
于 2013-11-26T19:24:45.240 に答える