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