简体   繁体   English

如何使用C#中的WMI按文件名搜索远程系统上的文件?

[英]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. 另请下载WMI代码创建器(请检查Google,因为我的信誉<10而无法链接它)以轻松测试您的WMI查询。

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

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

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