简体   繁体   English

以编程方式确定在Windows上上次修改文件的用户?

[英]Programmatically determine user who last modified file on Windows?

I've been tasked with writing a simple command line utility in C# that will monitor a directory on a server that several users will be accessing to copy/cut/paste/view data. 我的任务是在C#中编写一个简单的命令行实用程序,它将监视服务器上的一个目录,该目录将由多个用户访问以复制/剪切/粘贴/查看数据。 I used FileSystemWatcher to do this but it's lacking a couple features. 我使用FileSystemWatcher来做这个,但它缺少一些功能。

Is it possible to determine the user or at least the computer name from where the file is being accessed/modified? 是否可以确定用户或至少从访问/修改文件的位置确定计算机名称

(Note: This doesn't have to be with FileSystemWatcher, I'm looking for ANY way to do this.) (注意:这不一定是FileSystemWatcher,我正在寻找任何方法来做到这一点。)

I don't think you'll be able to monitor this from C# directly. 我认为你不能直接从C#监听这个。 Not without the help of the host operating system anyway. 无论如何,并非没有主机操作系统的帮助。 Windows and NTFS allow you to audit a particular directory and log the accesses in the Security event log for the host machine (so the server hosting the share would have to audit, not the client). Windows和NTFS允许您审核特定目录并在主机的安全事件日志中记录访问(因此托管共享的服务器必须审核,而不是客户端)。

From KB310399 - How to audit user access of files, folders, and printers in Windows XP 来自KB310399 - 如何在Windows XP中审核用户对文件,文件夹和打印机的访问

Auditing User Access of Files, Folders, and Printers 审核文件,文件夹和打印机的用户访问权限

The audit log appears in the Security log in Event Viewer. 审核日志显示在事件查看器的安全日志中。 To enable this feature: 要启用此功能:

  1. Click Start, click Control Panel, click Performance and Maintenance, and then click Administrative Tools. 依次单击“开始”,“控制面板”,“性能和维护”,然后单击“管理工具”。
  2. Double-click Local Security Policy. 双击“本地安全策略”。
  3. In the left pane, double-click Local Policies to expand it. 在左窗格中,双击“本地策略”以展开它。
  4. In the left pane, click Audit Policy to display the individual policy settings in the right pane. 在左窗格中,单击“审核策略”以在右窗格中显示各个策略设置。
  5. Double-click Audit object access. 双击“审核对象访问”。
  6. To audit successful access of specified files, folders and printers, select the Success check box. 要审核指定文件,文件夹和打印机的成功访问,请选中“成功”复选框。
  7. To audit unsuccessful access to these objects, select the Failure check box. 要审核对这些对象的不成功访问,请选中“失败”复选框。
  8. To enable auditing of both, select both check boxes. 要启用两者的审核,请选中两个复选框。
  9. Click OK. 单击确定。

Specifying Files, Folders, and Printers to Audit 指定要审核的文件,文件夹和打印机

After you enable auditing, you can specify the files, folders, and printers that you want audited. 启用审核后,您可以指定要审核的文件,文件夹和打印机。 To do so: 为此:

  1. In Windows Explorer, locate the file or folder you want to audit. 在Windows资源管理器中,找到要审核的文件或文件夹。 To audit a printer, locate it by clicking Start, and then clicking Printers and Faxes. 要审核打印机,请单击“开始”,然后单击“打印机和传真”以找到它。
  2. Right-click the file, folder, or printer that you want to audit, and then click Properties. 右键单击要审核的文件,文件夹或打印机,然后单击“属性”。
  3. Click the Security tab, and then click Advanced. 单击“安全”选项卡,然后单击“高级”。
  4. Click the Auditing tab, and then click Add. 单击“审核”选项卡,然后单击“添加”。
  5. In the Enter the object name to select box, type the name of the user or group whose access you want to audit. 在“输入要选择的对象名称”框中,键入要审核其访问权限的用户或组的名称。 You can browse the computer for names by clicking Advanced, and then clicking Find Now in the Select User or Group dialog box. 单击“高级”,然后在“选择用户或组”对话框中单击“立即查找”,可以浏览计算机以查找名称。
  6. Click OK. 单击确定。
  7. Select the Successful or Failed check boxes for the actions you want to audit, and then click OK. 选择要审核的操作的“成功”或“失败”复选框,然后单击“确定”。
  8. Click OK, and then click OK. 单击“确定”,然后单击“确定”。

The process is similar for the server operating systems and Windows Vista/Windows 7. If you go this route, you can have the C# program read the event log (See EventLog class) to look for the data you want. 服务器操作系统和Windows Vista / Windows 7的过程类似。如果你走这条路,你可以让C#程序读取事件日志(参见EventLog类)来查找你想要的数据。

Note: Starting with vista you must be and (UAC elevated if needed) administrator to read them from code. 注意:从vista开始,您必须(并在需要时提升UAC)管理员才能从代码中读取它们。

Make sure to have WMI installed or enabled on your PC, also make sure to add a reference to System.Management and System.Management.Instrumentation as well. 确保在PC上安装或启用了WMI,同时确保添加对System.ManagementSystem.Management.Instrumentation的引用。 There is also a C# and VB WMI scripting application GUI that you can download to run and test WMI Queries against as well Google that one. 还有一个C#和VB WMI脚本应用程序GUI,您可以下载它以运行和测试WMI查询以及Google那个。 Since I work for Dept of Defense there are certain things that I can get to from here in regards to the web other things are blocked out so please forgive me if I don't post certain web links. 由于我在国防部工作,我可以从这里得到一些关于网络的其他事情被阻止的事情所以请原谅我,如果我不发布某些网络链接。

Here is something to get you started 这是让你入门的东西

    ManagementScope mgtScope = new ManagementScope("\\\\ComputerName\\root\\cimv2");
    // you could also replace the username in the select with * to query all objects
    ObjectQuery objQuery = new ObjectQuery("SELECT username FROM Win32_ComputerSystem");

    ManagementObjectSearcher srcSearcher = new ManagementObjectSearcher(mgtScope, objQuery);

    ManagementObjectCollection colCollection = srcSearcher.Get();

    foreach (ManagementObject curObjCurObject in colCollection)
    {

        Console.WriteLine(curObjCurObject["username"].ToString());
    } 

  //if you want ot get the name of the machine that changed it once it gets into that  Event change the query to look like this. I just tested this locally and it does work 

    ManagementObjectSearcher mosQuery = new ManagementObjectSearcher("SELECT * FROM Win32_Process WHERE ProcessId = " + Process.GetCurrentProcess().Id.ToString());
    ManagementObjectCollection queryCollection1 = mosQuery.Get();
    foreach (ManagementObject manObject in queryCollection1)
    {
        Console.WriteLine("Name : " + manObject["name"].ToString());
        Console.WriteLine("Version : " + manObject["version"].ToString());
        Console.WriteLine("Manufacturer : " + manObject["Manufacturer"].ToString());
        Console.WriteLine("Computer Name : " + manObject["csname"].ToString());
        Console.WriteLine("Windows Directory : " + manObject["WindowsDirectory"].ToString());
    }  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM