开发者

Wake System from Sleep C#

开发者 https://www.devze.com 2023-01-18 21:15 出处:网络
I\'m trying to add functionality in my program that will allow the user to wake their s开发者_如何转开发ystem from sleep at a set duration.

I'm trying to add functionality in my program that will allow the user to wake their s开发者_如何转开发ystem from sleep at a set duration.

I've googled a lot about this and the examples online don't seem to work.

I've used WaitableTimer set the system to go to sleep but it doesn't seem to wake up.

Can anyone help me out here.

for code reference, I am using WPF

Thanks


Is any other software able to wake your computer on schedule? You need to find out whether the problem is with your code or with your system configuration, and running someone else's software as a test is the easiest way.

You may need to enable "Wake on Timer" support in the BIOS.

What version of Windows are you using? Windows Vista and 7 come with some tools for enabling/disabling the ability of individual components to wake the system, in order to e.g. resolve problems when a very sensitive mouse unintentionally moves enough to wake the computer. You may need to enable wake support for the HPET or RTC components.


Have you tried Wake On LAN (using MAC address):

namespace WakeOnLan
{
    class Program
    {
        static void Main(string[] args)
        {
            if(args.Length==1)
            {
                string bytes = args[0].Replace("-", "");
                long l = 0;
                if (bytes.Length != 12 || !long.TryParse(bytes.Substring(0, 6), NumberStyles.HexNumber, null, out l) || !long.TryParse(bytes.Substring(6), NumberStyles.HexNumber, null, out l))
                    Console.WriteLine("Invalid string");
                else
                {
                    try
                    {
                        WakeOnLan(bytes);
                        Console.WriteLine("Sent wake on lan");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                }
            }
            else
            {
                Console.WriteLine("WakeOnLan.exe <MAC Address>\r\nMAC address must be 6 bytes in hexadecimal format, optionally separated by hyphen.");
            }
        }

        private static void WakeOnLan(string bytesString)
        {
            List<byte> packet = new List<byte>();
            for (int i = 0; i < 6; i++)
            {
                packet.Add(byte.Parse(bytesString.Substring(i*2,2),NumberStyles.HexNumber));
            }

            byte[] mac = packet.ToArray();
            for (int i = 0; i < 15; i++)
            {
                packet.AddRange(mac);
            }
            for (int i = 0; i < 6; i++)
            {
                packet.Insert(0, 0xFF);
            }

            UdpClient client = new UdpClient();
            client.Connect(IPAddress.Broadcast, 7); //Any UDP port will work, but 7 is my lucky number ... 
            client.Send(packet.ToArray(), packet.Count); 
        }

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消