using System.Diagnostics; using System.Net; using System.Net.Sockets; using System.Text; namespace WindowLock { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// 启动tcp,接收wake命令后,唤醒屏幕 private void button1_Click(object sender, EventArgs e) { Thread thread = new Thread(new ThreadStart(StartTcp)); thread.Start(); } private void StartTcp() { TcpListener listener = new TcpListener(IPAddress.Any, 8888); listener.Start(); while (true) { TcpClient client = listener.AcceptTcpClient(); NetworkStream stream = client.GetStream(); byte[] buffer = new byte[1024]; int length = stream.Read(buffer, 0, buffer.Length); string message = Encoding.UTF8.GetString(buffer, 0, length); if (message == "wake") { WakeUp(); } stream.Close(); client.Close(); } } /// 蓝牙唤醒,接收到 wake 命令后,执行 WwakeUp 函数 private void button2_Click(object sender, EventArgs e) { Thread thread = new Thread(new ThreadStart(StartBluetooth)); thread.Start(); } /// private void StartBluetooth() { /// 检测蓝牙是否打开 BluetoothRadio radio = BluetoothRadio.PrimaryRadio; if (radio == null) { MessageBox.Show("蓝牙未打开"); return; } BluetoothListener listener = new BluetoothListener(new Guid("00001101-0000-1000-8000-00805F9B34FB")); listener.Start(); while (true) { BluetoothClient client = listener.AcceptBluetoothClient(); NetworkStream stream = client.GetStream(); byte[] buffer = new byte[1024]; int length = stream.Read(buffer, 0, buffer.Length); string message = Encoding.UTF8.GetString(buffer, 0, length); if (message == "wake") { WakeUp(); } stream.Close(); client.Close(); } } /// 屏幕唤醒 private void WakeUp() { Process.Start("C:\\Windows\\System32\\scrnsave.scr", "/s"); Thread.Sleep(1000); SendKeys.SendWait("{ENTER}"); } } }