简体   繁体   中英

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.

I tried and incorporated changes discussed in remote wmi connection c# - invalid parameter error

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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