2015-04-22 75 views
0
using System; 
using System.Management; 

public class Class1 
{ 
    public static void Main() 
    { 
     string strComputer = string.Format(@"machineName.domainname\root\cimv2"); 
     ConnectionOptions options = new ConnectionOptions(); 
     options.EnablePrivileges = true; 
     options.Impersonation = ImpersonationLevel.Impersonate; 
     options.Authentication = AuthenticationLevel.Packet; 
     options.Authority = "ntlmdomain:InsTIL.com:InsTIL.com"; 
     options.Username = "usr"; 
     options.Password = "pwd"; 

     ManagementScope oMs = new ManagementScope(strComputer, options); 

     SelectQuery query =new SelectQuery("Select * From Win32_Directory Where Name ='"+string.Format(@"C:\Scripts")+"'"); 
     ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs,query); 
     ManagementObjectCollection oReturnCollection = oSearcher.Get(); 

     if (oReturnCollection.Count < 1) 
     { 
      Console.WriteLine("Folder does not exist"); 
     } 
     else 
     { 
      Console.WriteLine("Folder does exist"); 
     } 

    } 
} 

我想連接到遠程機器並檢查是否存在folder.But我得到下面提到的錯誤。WMI連接錯誤C#

我嘗試並納入remote wmi connection c# - invalid parameter error

程序突然停止工作,討論修改,並拋出以下錯誤:

Unhandled Exception: System.Management.ManagementException: Invalid parameter 
    at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStat 
us errorCode) 
    at System.Management.ManagementPath.CreateWbemPath(String path) 
    at System.Management.ManagementPath..ctor(String path) 
    at Class1.Main() 
+0

你爲什麼要使用的String.Format不帶任何參數:更改此:

string strComputer = string.Format(@"machineName.domainname\root\cimv2"); 

這樣做呢?你到底發生了什麼錯誤?有沒有內部的例外? –

+0

另外你的NTLM域不會和那個問題一樣,所以如果你不在域上,或者至少使用正確的域名,我會避免使用NTLM域名。 –

回答

0

您的機器名前需要反斜槓。

string strComputer = string.Format(@"\\machineName.domainname\root\cimv2");