2015-04-22 66 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");