zoukankan      html  css  js  c++  java
  • C#调用WMI关机示例

    WMI中Win32_OperationSystem的方法Win32ShutDown(flag)中flag的参数可以是下表中的任意一种:

    值 描述
    0 注销
    0 + 4 强制注销
    1 关机
    1 + 4 强制关机
    2 重起
    2 + 4 强制重起
    8 关闭电源
    8 + 4 强制关闭电源

    下面是示例:

    //关闭计算机
    private void btn_Shutdown_Click(object sender, EventArgs e)
    {
        string IPShutdown = "192.168.1.100";

        DialogResult dlResult = MessageBox.Show("确实要关闭“" + IPShutdown + "”电源吗?", "请确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        if (dlResult == DialogResult.Yes)
        {
            string[] inParams ={ "8", "4" };
            BootComputer ShutdownBootComputer = new BootComputer();
            ShutdownBootComputer.strIp = IPShutdown;
            ShutdownBootComputer.strAdmin = txtAdmin.Text.Trim();
            ShutdownBootComputer.strPassword = txtPassword.Text.Trim();
            ShutdownBootComputer.strMothod = "Win32Shutdown";
            ShutdownBootComputer.inParams = inParams;
            ShutdownBootComputer.BootMachine();
        }
    }

    //关闭重启计算机(支持多线程)
    public class BootComputer
    {
        public string strIp, strAdmin, strPassword, strMothod;
        public string[] inParams;
        public void BootMachine()
        {
            ConnectionOptions BootConn = new ConnectionOptions();
            BootConn.Username = strAdmin;
            BootConn.Password = strPassword;
            ManagementScope ms = new ManagementScope("\\\\" + strIp + "\\root\\cimv2", BootConn);
            ms.Options.EnablePrivileges = true;
            if (!string.IsNullOrEmpty(strAdmin) && !string.IsNullOrEmpty(strPassword))
            {
                try { ms.Connect(); }
                catch { }
            }
            if (ms.IsConnected)
            {
                try
                {
                    ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
                    ManagementObjectSearcher mos = new ManagementObjectSearcher(ms, oq);
                    ManagementObjectCollection moc = mos.Get();
                    foreach (ManagementObject mo in moc)
                    {
                        string[] ss = inParams;
                        mo.InvokeMethod(strMothod, ss);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(strIp + ":" + ex.Message + "网络不通或用户名、密码不正确!");
                }
            }
        }
    }

  • 相关阅读:
    滚动监听+导航固顶
    选项卡 || 图片切换
    绝对定位下如何居中?
    选项卡+轮播的实现
    设置mysql 及其他应用程序 自动启动
    边框阴影 模糊值 x轴偏移值 y轴偏移值 模糊半径 阴影半径 || 颜色 insect,其中阴影半径可以为负值,意思是增加或减少指定数值的阴影半径
    form表单中的 下拉菜单 所有的省份
    媒体查询 屏幕超过页面上版心的宽度时 ,(也就是所有内容能显示出来),不让它有滚动条 【解决了因为banner图的原因出现滚动条的问题】
    jenkins 安全权限及注册新的测试角色使用
    git基础概念(和svn的优劣)
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1984550.html
Copyright © 2011-2022 走看看