簡體   English   中英

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

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

如何使用WMI和C#通過文件名在遠程系統上搜索文件?

試試這個代碼並檢查一下 另請下載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