WMI连接错误C#

时间:2015-04-22 18:53:51

标签: c# wmi

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

    }
}

我正在尝试连接到远程计算机并检查文件夹的存在。但是我收到了下面提到的错误。

我尝试并纳入了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()

1 个答案:

答案 0 :(得分:0)

您的机器名称前需要反斜杠。改变这个:

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

到此:

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