Programmatically determine user who last modified file on Windows?

后端 未结 2 1474
面向向阳花
面向向阳花 2020-12-29 10:15

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

2条回答
  •  余生分开走
    2020-12-29 11:10

    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. 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. 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());
        }  
    

提交回复
热议问题