C# 远程开机 代码

一、BIOS配置

1、关机状态下禁止休眠
2、允许网卡唤醒

二、网卡管理

开机状态下,电源管理,
禁止休眠
允许网络唤醒
允许魔术包唤醒

三、代码

using System;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;

namespace CommonPro
{
    public class MagicPack
    {
        private static IPEndPoint point;

        private static readonly UdpClient client = new UdpClient();

        /**
         * 唤醒远程机器方法
         * @param
         * mac 要唤醒的机器的MAC
         * IP
         * port udp消息发送端口
         *
         * 摘要:唤醒方法为网卡提供的魔术封包功能,即以广播模式发送6个FF加上16遍目标MAC地址的字节数组
         **/
        public static void wakeUp(string mac, int port, string ip)
        {
            var magicBytes = getMagicPacket(mac);
            point = new IPEndPoint(IPAddress.Parse(ip), port); //广播模式:255.255.255.255
            try
            {
                client.Send(magicBytes, magicBytes.Length, point);
            }
            catch (SocketException e)
            {
                MessageBox.Show(e.Message);
            }
        }

        /// <summary>
        ///     字符串转16进制字节数组
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static byte[] strToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            hexString = hexString.Replace("-", "");
            if (hexString.Length % 2 != 0)
                hexString += " ";
            var returnBytes = new byte[hexString.Length / 2];
            for (var i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }

        /// <summary>
        ///     拼装MAC魔术封包
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static byte[] getMagicPacket(string macString)
        {
            var returnBytes = new byte[102];
            var commandString = "FFFFFFFFFFFF";
            for (var i = 0; i < 6; i++)
                returnBytes[i] = Convert.ToByte(commandString.Substring(i * 2, 2), 16);
            var macBytes = strToHexByte(macString);
            for (var i = 6; i < 102; i++) returnBytes[i] = macBytes[i % 6];
            return returnBytes;
        }

        /// <summary>
        ///     获得广播地址
        /// </summary>
        /// <param name="ipAddress">IP地址</param>
        /// <param name="subnetMask">子网掩码</param>
        /// <returns>广播地址</returns>
        public static string GetBroadcast(string ipAddress, string subnetMask)
        {
            if (string.IsNullOrEmpty(ipAddress)) return IPAddress.Broadcast.ToString();

            if (string.IsNullOrEmpty(subnetMask)) return IPAddress.Broadcast.ToString();

            var ip = IPAddress.Parse(ipAddress).GetAddressBytes();
            var sub = IPAddress.Parse(subnetMask).GetAddressBytes();

            // 广播地址=子网按位求反 再 或IP地址 
            for (var i = 0; i < ip.Length; i++) ip[i] = (byte) (~sub[i] | ip[i]);
            return new IPAddress(ip).ToString();
        }
    }
}

本站原创,未经允许不得转载:lingkb » C# 远程开机 代码

下载信息

远程开机源码下载
最近更新2019年10月30日
在线咨询
赞(0)

相关推荐

评论抢沙发