windows系统操作类和演示程序(关机,关闭显示器,打开屏幕保护程序,打开光驱等)

/// <summary>
/// 系统控制类,关机,关闭显示器,打开屏幕保存程序等
/// </summary>
public class SystemPowerController
{
private const int WM_SYSCOMMAND = 0x112; //系统消息
private const int SC_SCREENSAVE = 0xF140; //启动屏幕保护消息
private const int SC_MONITORPOWER = 0xF170; //关闭显示器的系统命令
private static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff); //广播消息,所有*窗体都会接收 [System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); [System.Runtime.InteropServices.DllImport(@"user32.dll", EntryPoint = "ExitWindowsEx", CharSet = System.Runtime.InteropServices.CharSet.Ansi)]
private static extern int ExitWindowsEx(int uFlags, int dwReserverd); [System.Runtime.InteropServices.DllImport("winmm.dll", EntryPoint = "mciSendStringA")]
private static extern int mciSendString(string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLenght, IntPtr hwndCallback); /// <summary>
/// 显示器操作,打开、关闭、省电状态
/// </summary>
/// <param name="power">指示对显示器用何种操作</param>
public void MonitorPower(MonitorPowerType power)
{
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (int)power);
} /// <summary>
/// 开启屏幕保护程序
/// </summary>
public void ScreenSave()
{
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_SCREENSAVE, 0);
} /// <summary>
/// 系统操作,注销windows、关机、重启
/// </summary>
/// <param name="exit">指示对windows进行何种操作</param>
public void ExitWindows(ExitWindowsType exit)
{
try
{
switch (exit)
{
case ExitWindowsType.Exit://注销
ExitWindowsEx(0, 0);
break;
case ExitWindowsType.Off://关机
HideShutdownUi(" -s -t 0");
break;
case ExitWindowsType.Restart://重启
HideShutdownUi(" -r -t 0");
break;
}
}
catch (Exception)
{
throw;
}
} private void HideShutdownUi(string arguments)
{
//声明一个程序信息类
System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
//设置外部程序名
Info.FileName = "Shutdown.exe";
//设置隐藏窗口
Info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//设置外部程序的启动参数(命令行参数)为test.txt
Info.Arguments = arguments;
//设置外部程序工作目录为 C:\
//Info.WorkingDirectory = "C:\\";
//声明一个程序类
System.Diagnostics.Process Proc;
try
{
//
//启动外部程序
//
Proc = System.Diagnostics.Process.Start(Info);
}
catch (System.ComponentModel.Win32Exception exc)
{
throw exc;
}
} /// <summary>
/// 打开或关闭光驱
/// </summary>
/// <param name="open">true:打开,false:关闭</param>
public void CDDriver(bool open)
{
if (open)
mciSendString("set cdaudio door open", null, 0, IntPtr.Zero);
else
mciSendString("set cdaudio door closed", null, 0, IntPtr.Zero);
}
} /// <summary>
/// 显示器操作枚举
/// </summary>
public enum MonitorPowerType
{
/// <summary>
/// 打开显示器
/// </summary>
POWER_ON = -1,
/// <summary>
/// 进入省电状态
/// </summary>
POWER_SAVE = 1,
/// <summary>
/// 关闭显示器
/// </summary>
POWER_OFF = 2
} /// <summary>
/// 系统操作枚举
/// </summary>
public enum ExitWindowsType
{
/// <summary>
/// 注销windows
/// </summary>
Exit,
/// <summary>
/// 关机
/// </summary>
Off,
/// <summary>
/// 重启系统
/// </summary>
Restart
}

自己在网上查阅的所需资源整理后封装成的windows系统操作类,测试全通过,放心使用。

上一篇:js算法运算


下一篇:Docker 发布 Abp net core web 服务