using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace Start10
{
public class SystemHelper
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
[DllImport("kernel32.dll", ExactSpelling = true)]
internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool ExitWindowsEx(int flg, int rea);
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
/// <summary>
/// 包含这个参数可以让系统强制关机,可能会让应用程序丢失数据
/// </summary>
internal const int EWX_FORCE = 0x00000004;
/// <summary>
///
/// </summary>
internal const int EWX_POWEROFF = 0x00000008;
/// <summary>
/// 如果在超时时间以后应用进程仍然没有响应WM_QUERYENDSESSION或WM_ENDSESSION消息,那么就强制关闭它们。
/// </summary>
internal const int EWX_FORCEIFHUNG = 0x00000010;
public static void DoExitWin(int flg)
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
ok = ExitWindowsEx(flg, 0);
}
/// <summary>
/// 锁屏
/// </summary>
/// <returns></returns>
[DllImport("user32 ")]
public static extern bool LockWorkStation();//这个是调用windows的系统锁定
/// <summary>
/// WTSDisconnectSession来断开(而不是终止)当前会话,从而进入用户切换界面。
/// </summary>
/// <param name="hServer"></param>
/// <param name="sessionId"></param>
/// <param name="bWait"></param>
/// <returns></returns>
[DllImport("Wtsapi32.dll", SetLastError = true)]
extern static bool WTSDisconnectSession(IntPtr hServer, int sessionId, [MarshalAs(UnmanagedType.Bool)] bool bWait);
public static void switchSystemUsersMethod()
{
IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;
int WTS_CURRENT_SESSION = -1;
if (!WTSDisconnectSession(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, false))
{
Console.WriteLine("WTSDisconnectSession Failed: " + Marshal.GetLastWin32Error());
}
}
const uint SPI_GETSCREENSAVERRUNNING = 0x0072;
const uint SPI_GETSCREENSAVEACTIVE = 0x0010;
const uint SPI_SETSCREENSAVEACTIVE = 0x0011;
const uint SPIF_SENDCHANGE = 0x0002;
const uint SPIF_SENDWININICHANGE = SPIF_SENDCHANGE;
const uint SPI_GETDESKWALLPAPER = 0x0073;
const uint SPI_SETDESKWALLPAPER = 0x0014;
const uint SPIF_UPDATEINIFILE = 0x001;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool SystemParametersInfo(uint uAction, uint uParam, StringBuilder lpvParam, uint init);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref bool pvParam, uint fWinIni);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool SystemParametersInfo(uint uiAction, uint uiParam, string path, uint fWinIni);
/// <summary>
/// 判读系统屏保是否正在运行
/// </summary>
/// <returns></returns>
public static bool IsScreenSaverRunning()
{
bool isRun = false;
SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, ref isRun, 0);
return isRun;
}
/// <summary>
/// 设置系统进程模式
/// </summary>
/// <param name="esFlags"></param>
/// <returns></returns>
[DllImport("kernel32.dll")]
static extern uint SetThreadExecutionState(uint esFlags);
const uint ES_SYSTEM_REQUIRED = 0x00000001;
const uint ES_DISPLAY_REQUIRED = 0x00000002;
const uint ES_CONTINUOUS = 0x80000000;
/// <summary>
/// 播放时调用,禁止系统休眠
/// </summary>
public static void SetSystemMode()
{
//播放时调用,
SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED);
}
/// <summary>
/// 恢复系统正常
/// </summary>
public static void SetSystemNormalMode()
{
//播放结束后调用 ,恢复系统正常
SetThreadExecutionState(ES_CONTINUOUS);
}
/// <summary>
/// 设置桌面壁纸
/// </summary>
/// <param name="path"></param>
public static void SetWallPaper(string path)
{
SystemParametersInfo(SPI_SETDESKWALLPAPER,
0, path,
SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
}
/// <summary>
/// 获取桌面壁纸图片路径
/// </summary>
/// <returns></returns>
public static string GetWallPaper()
{
StringBuilder wallPaperPath = new StringBuilder(200);
if (SystemParametersInfo(SPI_GETDESKWALLPAPER, 200, wallPaperPath, 0))
{
return wallPaperPath.ToString();
}
return "";
}
}
}
调用:
string path = SystemHelper.GetWallPaper();
string path2 = @"C:\Users\Administrator\Desktop\white.png";
SystemHelper.SetWallPaper(path2);