首页上一页 1 下一页尾页 1 条记录 1/1页
代码请教
发表在C#图书答疑
2016-06-13
是否精华
是
否
版块置顶:
是
否
运行远程控制计算机这个案例时,老报上图错!代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;
namespace guanji
{
public partial class Form6 : Form
{
private object op;
public Form6()
{
InitializeComponent();
}
private void Form6_Load(object sender, EventArgs e)
{
}
private void CloseComputer(string strname, string strpwd, string ip, string doinfo)
{
ConnectionOptions op = new ConnectionOptions(); //实例化ConnectionOptions
op.Username = strname; //'的账号(注意要有管理员的权限)
op.Password = strpwd; //密码
ManagementScope scope = new ManagementScope("\\\\" + ip + "\\root\\cimv2:Win32_Service", op);
try
{
scope.Connect(); //连接到实际的WMI范围
System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem"); //实例化ObjectQuery
ManagementObjectSearcher query1 = new ManagementObjectSearcher(scope, oq);
ManagementObjectCollection queryCollection1 = query1.Get(); //得到WMI控制
foreach (ManagementObject mobj in queryCollection1) //遍历所有得到的WMI信息
{
string[] str = { "" }; //声明数组存储信息
mobj.InvokeMethod(doinfo, str); //根据参数doinfo执行不同的操作(关机或重启)
}
MessageBox.Show("操作成功"); //如果操作成功弹出提示
}
catch (Exception ey) //如果发生异常
{
MessageBox.Show(ey.Message); //显示异常信息
this.button1.PerformClick(); //生成按钮的Click事件
}
}
//
}
internal class ConnectionOptions
{
public string Password { get; internal set; }
public string Username { get; internal set; }
}
internal class ManagementObject
{
internal void InvokeMethod(string doinfo, string[] str)
{
throw new NotImplementedException();
}
}
internal class ManagementObjectCollection
{
}
internal class ManagementObjectSearcher
{
private ObjectQuery oq;
private ManagementScope scope;
public ManagementObjectSearcher(ManagementScope scope, ObjectQuery oq)
{
this.scope = scope;
this.oq = oq;
}
internal ManagementObjectCollection Get()
{
throw new NotImplementedException();
}
}
}
namespace System.Management
{
class ObjectQuery
{
private string v;
public ObjectQuery(string v)
{
this.v = v;
}
}
}
控制台提示:Severity Code Description Project File Line Suppression State
Error CS1579 foreach statement cannot operate on variables of type 'guanji.ManagementObjectCollection' because 'guanji.ManagementObjectCollection' does not contain a public definition for 'GetEnumerator' guanji D:\My Documents\Visual Studio 2015\Projects\guanji\guanji\Form6.cs 51 Active
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;
namespace guanji
{
public partial class Form6 : Form
{
private object op;
public Form6()
{
InitializeComponent();
}
private void Form6_Load(object sender, EventArgs e)
{
}
private void CloseComputer(string strname, string strpwd, string ip, string doinfo)
{
ConnectionOptions op = new ConnectionOptions(); //实例化ConnectionOptions
op.Username = strname; //'的账号(注意要有管理员的权限)
op.Password = strpwd; //密码
ManagementScope scope = new ManagementScope("\\\\" + ip + "\\root\\cimv2:Win32_Service", op);
try
{
scope.Connect(); //连接到实际的WMI范围
System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem"); //实例化ObjectQuery
ManagementObjectSearcher query1 = new ManagementObjectSearcher(scope, oq);
ManagementObjectCollection queryCollection1 = query1.Get(); //得到WMI控制
foreach (ManagementObject mobj in queryCollection1) //遍历所有得到的WMI信息
{
string[] str = { "" }; //声明数组存储信息
mobj.InvokeMethod(doinfo, str); //根据参数doinfo执行不同的操作(关机或重启)
}
MessageBox.Show("操作成功"); //如果操作成功弹出提示
}
catch (Exception ey) //如果发生异常
{
MessageBox.Show(ey.Message); //显示异常信息
this.button1.PerformClick(); //生成按钮的Click事件
}
}
//
}
internal class ConnectionOptions
{
public string Password { get; internal set; }
public string Username { get; internal set; }
}
internal class ManagementObject
{
internal void InvokeMethod(string doinfo, string[] str)
{
throw new NotImplementedException();
}
}
internal class ManagementObjectCollection
{
}
internal class ManagementObjectSearcher
{
private ObjectQuery oq;
private ManagementScope scope;
public ManagementObjectSearcher(ManagementScope scope, ObjectQuery oq)
{
this.scope = scope;
this.oq = oq;
}
internal ManagementObjectCollection Get()
{
throw new NotImplementedException();
}
}
}
namespace System.Management
{
class ObjectQuery
{
private string v;
public ObjectQuery(string v)
{
this.v = v;
}
}
}
控制台提示:Severity Code Description Project File Line Suppression State
Error CS1579 foreach statement cannot operate on variables of type 'guanji.ManagementObjectCollection' because 'guanji.ManagementObjectCollection' does not contain a public definition for 'GetEnumerator' guanji D:\My Documents\Visual Studio 2015\Projects\guanji\guanji\Form6.cs 51 Active