隐藏

C# 设置电脑关机、重启、注销、锁定、关闭显示器的类(支持win7,win8)

发布:2020/4/14 11:02:41作者:管理员 来源:本站 浏览次数:1162

public class MyComputer
    {      
       [DllImport("user32")]
       public static extern bool ExitWindowsEx(uint uFlags,uint dwReason);
       [DllImport("user32")]
       public static extern void LockWorkStation();
       [DllImport("user32")]
       public static extern int SendMessage(int hWnd,int hMsg,int wParam,int lParam);
       public enum MonitorState
       {
           MonitorStateOn=-1,
           MonitorStateOff=2,
           MonitorStateStandBy=1
       }
       public static void ShutDown()
       {
           try
           {
               System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo("shutdown.exe", "-s -t 00");
               System.Diagnostics.Process.Start(startinfo);
           }
           catch { }
       }
       public static void Restart()
       {
           try
           {
               System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo("shutdown.exe", "-r -t 00");
               System.Diagnostics.Process.Start(startinfo);
           }
           catch { }
       }
       public static void LogOff()
       {
           try
           {
               ExitWindowsEx(0, 0);
           }
           catch { }
       }
       public static void LockPC()
       {
           try
           {
               LockWorkStation();
           }
           catch { }
       }
       public static void Turnoffmonitor()
       {
           SetMonitorInState(MonitorState.MonitorStateOff);
       }
       private static void SetMonitorInState(MonitorState state)
       {
           SendMessage(0xFFFF, 0x112, 0xF170, (int)state);
       }




    }
————————————————