已有57人关注
代码请教
发表在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
分享到:
精彩评论 1
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照