三度网教程:是一个免费提供流行视频软件教程、在线学习分享的学习平台!

C#远程关机 C#远程关机代码

时间:2022-11-21作者:未知来源:三度网教程人气:


本站电脑知识提供电脑入门知识,计算机基础知识,计算机网络应用基础知识,电脑配置知识,电脑故障排除和电脑常识大全,帮助您更好的学习电脑!不为别的,只因有共同的爱好,为中国互联网发展出一分力!

C#远程关机 C#远程关机代码

 

WMIWin32_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 + "
网络不通或用户名、密码不正确!");
            }
        }
    }
}

 


学习教程快速掌握从入门到精通的电脑知识

关键词:  C#远程关机  C#远程关机代码





Copyright © 2012-2018 三度网教程(http://www.3du8.cn) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版