在C#中使用FileSystemWatcher时如何获取用户名? [英] How can I obtain the username when using a FileSystemWatcher in C#
问题描述
我正在使用FileSystemWatcher来监控网络上的共享文件夹。三台PC将共享此文件夹,并观看谁访问了什么。
我可以通过使用GetAccessControl函数读取用户名'创建功能'
I am using a FileSystemWatcher to monitor a share folder on network. Three PC will share this folder and watch who accessing what.
I could read user name by using GetAccessControl function for 'Create function'
protected void mywatcher_created(object sender,FileSystemEventArgs e)
{
string user_name = System.IO.File.GetAccessControl(e.FullPath).GetOwner(typeof(System.Security.Principal.NTAccount)).ToString();
DateTime current = DateTime.Now;
lstCreate.Items.Add(e.FullPath.ToString());
lstCreate.Items[cCount].SubItems.Add(current.ToShortDateString());
lstCreate.Items[cCount].SubItems.Add(current.ToShortTimeString());
lstCreate.Items[cCount].SubItems.Add(user_name.ToString());
cCount += 1;
}
如果我想读取已删除,重命名或更改的文件的用户名,则不能使用GetAssessControl函数。我得到了'filepath找不到'的错误。
那么如何读取共享文件夹的用户名。
我用这个函数写了删除文件,
If I want to read user name for deleted , renamed or changed file , and GetAssessControl Function couldn't be used for those. I got error of 'filepath couln't find'.
So how can I read the user name of share folder.
I wrote deleted file by this function ,
protected void mywatcher_deleted(object sender, FileSystemEventArgs e)
{
string user_name = System.IO.File.GetAccessControl(e.FullPath).GetOwner(typeof(System.Security.Principal.NTAccount)).ToString();
CheckForIllegalCrossThreadCalls = false;
DateTime current = DateTime.Now;
lstDelete.Items.Add(e.FullPath.ToString());
lstDelete.Items[dCount].SubItems.Add(current.ToShortDateString());
lstDelete.Items[dCount].SubItems.Add(current.ToShortTimeString());
lstDelete.Items[dCount].SubItems.Add(user_name.ToString());
dCount += 1;
}
推荐答案
由于NTFS不跟踪谁删除,重命名或修改文件,所以你无法获得用户名。它只跟踪谁拥有该文件。
此外,CheckForIllegalCrossThreadCalls = false
行是什么?你永远都不应该受到客户的惩罚,不管是出于何种目的使用这行代码。
Since NTFS doesn't track who deletes, renames, or modifies a file, so there's no way you can get the username. It only keeps track of who OWNS the file.
Also, what's with the "CheckForIllegalCrossThreadCalls = false
" line? You should NEVER EVER, under penalty of clients ripping your eyeballs out, use that line of code for any purpose whatsoever.
这篇关于在C#中使用FileSystemWatcher时如何获取用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!