C# 远程开机,关闭简单案例

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/taoerit/article/details/83538863

    最近有个需求,就是远程开机,网上搜了些方法,我选用的是网卡开机,也就是通过发送广播包唤醒电脑

  网卡唤醒要求:

     1  发送广播包: 6个“FF"+16个目标主机网卡地址

       广播发送bys数据即可

  

     2 BIOS要设置LAN唤醒

       有的需要设置本地连接的唤醒,其实基本不用设置,默认是开启的,主要是在bios里开启

 

了解网卡唤醒要求后,就可以写代码了

   2 主要代码

  public static void WakeUp(string macString)
        {
            try
            {
                if (macString.Split('-').Length == 6)
                {
                    string macStr = macString.Replace('-',' ');;

                    byte[] macByteArray = HexStringSToByteArray(macStr);
                 
                    UdpClient client = new UdpClient();
                    client.Connect(IPAddress.Broadcast, 1234);

                    byte[] packet = new byte[17 * 6];
                    for (int i = 0; i < 6; i++)
                    {
                        packet[i] = 0xFF;
                    }
                     
                    for (int i = 1; i <= 16; i++)
                    {
                        for (int ii = 0; ii < 6; ii++)
                        {
                            packet[i * 6 + ii] = macByteArray[ii];
                        } 
                    }

                    client.Send(packet, packet.Length);  
                }
            }
            catch (Exception ex)
            {
               // MessageBox.Show("网络唤醒失败:" + ex.StackTrace + " \r\n" + ex.Message);
            }
        }

3 字符串转换


        /// <summary>
        /// 2位16进制转int
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        public static int HexStringToInt(string hex)
        {
            int num1 = 0;
            int num2 = 0;
            char[] nums = hex.ToCharArray();
            if (hex.Length == 2)
            {
                for (int i = 0; i < nums.Length; i++)
                {
                    String strNum = nums[i].ToString().ToUpper();
                    switch (strNum)
                    {
                        case "A":
                            strNum = "10";
                            break;
                        case "B":
                            strNum = "11";
                            break;
                        case "C":
                            strNum = "12";
                            break;
                        case "D":
                            strNum = "13";
                            break;
                        case "E":
                            strNum = "14";
                            break;
                        case "F":
                            strNum = "15";
                            break;
                        default:

                            break;
                    }
                    if (i == 0)
                    {
                        num1 = int.Parse(strNum) * 16;
                    }
                    if (i == 1)
                    {
                        num2 = int.Parse(strNum);
                    }
                }
            }

            return num1 + num2;
        }


        /// <summary>
        /// 16进制字符串转byte[] 48 65 6C 6C 6F 20 57 6F 72 6C 64 21
        /// </summary>
        /// <param name="hexValues"></param>
        /// <returns></returns>
        public static byte[] HexStringSToByteArray(string hexValues)
        {

            string[] hexValuesSplit = hexValues.Split(' ');
            byte[] val = new byte[hexValuesSplit.Length];
            int i = 0;
            foreach (string hex in hexValuesSplit)
            {
                int value = HexStringToInt(hex);
                val[i] = (byte)value;
                i++;
            }

            return val;
        }

  4 调用方法

WakeUp("F0-79-59-68-36-49");

 5 远程关闭

    可用Socket通讯,发送指令,然后调用下面指令代码,可以关机


        /// <summary>
        /// 关闭系统
        /// </summary>
        /// <param name="time"></param>
        public static void CloseSystem(int time)
        {
            ProcessStartInfo ps = new ProcessStartInfo();
            ps.FileName = "shutdown.exe";
            ps.Arguments = "-s -t " + time;// -s -t 00
            Process.Start(ps);
        }

        /// <summary>
        /// 重启系统
        /// </summary>
        /// <param name="time"></param>
        public static void ReSystem(int time)
        {
            ProcessStartInfo ps = new ProcessStartInfo();
            ps.FileName = "shutdown.exe";
            ps.Arguments = "-r -t " + time;// -r -t 00
            Process.Start(ps);
        }

 

6 测试

   这一步很重要,如果你发广播自己都没收到,那就是有问题了,我也遇到了收不到的情况,最后的原因是自己的电脑有虚拟网卡,没有关闭掉,关掉就好了

展开阅读全文

没有更多推荐了,返回首页