简体   繁体   English

WMI连接错误C#

[英]WMI connection error C#

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

    }
}

I'm trying to connect to remote machine and checking existence of folder.But I'm getting below mentioned error. 我正在尝试连接到远程计算机并检查是否存在folder.But我得到下面提到的错误。

I tried and incorporated changes discussed in remote wmi connection c# - invalid parameter error 我尝试并合并了在远程WMI连接C#中讨论的更改-无效的参数错误

Program abruptly stops working and throws below 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()

You need backslashes before your machine name. 您需要在机器名称前加反斜杠。 Change this: 更改此:

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

to this: 对此:

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

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

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