Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数组在作用域处失败。Connect();如果输入为ManagementScope=new ManagementScope(string.Format(";\\\\\{0}\\root\\cimv2";,服务器,选项));_C#_Asp.net_Arrays_Wmi - Fatal编程技术网

C# 数组在作用域处失败。Connect();如果输入为ManagementScope=new ManagementScope(string.Format(";\\\\\{0}\\root\\cimv2";,服务器,选项));

C# 数组在作用域处失败。Connect();如果输入为ManagementScope=new ManagementScope(string.Format(";\\\\\{0}\\root\\cimv2";,服务器,选项));,c#,asp.net,arrays,wmi,C#,Asp.net,Arrays,Wmi,数组在scope.Connect()处失败;如果我将其输入为ManagementScope=newmanagementscope(string.Format(“\\{0}\root\cimv2”,servers,options));但如果我将其输入为服务器[0],则会通过。代码在服务器[0]上运行良好,但我需要在阵列中循环。有什么想法吗?提前谢谢 protected void ServerServices() { string txt = serverName.Text;

数组在scope.Connect()处失败;如果我将其输入为ManagementScope=newmanagementscope(string.Format(“\\{0}\root\cimv2”,servers,options));但如果我将其输入为服务器[0],则会通过。代码在服务器[0]上运行良好,但我需要在阵列中循环。有什么想法吗?提前谢谢

protected void ServerServices()
    {
        string txt = serverName.Text;
        string[] servers= txt.Split(new Char[] { '\n', '\r' }, 
        StringSplitOptions.RemoveEmptyEntries);

        ConnectionOptions options = new ConnectionOptions();
        options.Username = "myUsername";
        options.Password = "mypassword";
        options.EnablePrivileges = true;

        foreach (string item in servers)
        {
            //Create the scope that will enter code here connect to the 
             default `enter code here`root for WMI          
            ManagementScope scope = new ManagementScope(string.Format("\\\\
    `       enter code here`{0}\\root\\cimv2", servers[0], options));

            scope.Connect();
        }
}

您需要将
放入其中,而不是整个服务器集合

foreach (var item in servers)
{      
    var scope = new ManagementScope(
       string.Format(@"\\{0}\root\cimv2", item), options);
    scope.Connect();
}