简体   繁体   中英

How to search for a file on a remote system by file name using WMI in C#?

如何使用WMI和C#通过文件名在远程系统上搜索文件?

Try this code and check this . Download also WMI Code Creator ( check on google because I can't link it due to my reputation <10) to easily test your WMI query.

using System;
using System.Management;
namespace WMISample
{
    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
            ConnectionOptions oConn = new ConnectionOptions(); 
            oConn.Impersonation = ImpersonationLevel.Impersonate; 
            oConn.EnablePrivileges = true;
                string[] arrComputers = "clientName"};
                foreach (string strComputer in arrComputers)
                {
                    Console.WriteLine("==========================================");
                    Console.WriteLine("Computer: " + strComputer);
                    Console.WriteLine("==========================================");


                        ManagementObjectSearcher searcher = new ManagementObjectSearcher 
                        ( 
                           new ManagementScope("\\\\" + strComputer + "\\root\\CIMV2",  oConn), 
                           new ObjectQuery( @"SELECT * FROM CIM_DataFile WHERE Name = 'WhatyouWant.ToSearch'") 
                        ); 


                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("CIM_DataFile instance");
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Path: {0}", queryObj["Path"]);
                    }
                }
            }
            catch(ManagementException err)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + err.Message);
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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