欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#調(diào)用api控制windows關(guān)機(jī)示例(可以重啟/注銷)

 更新時間:2014年01月06日 10:54:39   作者:  
本文介紹了c#控制windows關(guān)機(jī)、重啟、注銷的二種方法,分為調(diào)用windows自帶的shutdown.exe關(guān)機(jī)和調(diào)用API關(guān)機(jī)的方法

方法一:調(diào)用windows自帶的shutdown.exe (缺點(diǎn):會出現(xiàn)倒計時窗口)

System.Diagnostics.Process.Start("shutdown.exe", "-r -f -t 15");

shutdown參數(shù)含義:-r關(guān)閉并重啟動此計算機(jī);-f 強(qiáng)制運(yùn)行的應(yīng)用程序關(guān)閉而沒有警告;-t 15 延時15shutdown.exe的詳細(xì)用法:
shutdown [-i | -l | -s | -r | -a] [-f] [-m //computername] [-t xx] [-c "comment"] [-d up:xx:yy]
沒有參數(shù) 顯示此消息(與 ? 相同)
-i 顯示 GUI 界面,必須是第一個選項
-l 注銷(不能與選項 -m 一起使用)
-s 關(guān)閉此計算機(jī)
-r 關(guān)閉并重啟動此計算機(jī)
-a 放棄系統(tǒng)關(guān)機(jī)
-m //computername 遠(yuǎn)程計算機(jī)關(guān)機(jī)/重啟動/放棄
-t xx 設(shè)置關(guān)閉的超時為 xx 秒
-c "comment" 關(guān)閉注釋(最大 127 個字符)
-f 強(qiáng)制運(yùn)行的應(yīng)用程序關(guān)閉而沒有警告
-d [u][p]:xx:yy 關(guān)閉原因代碼
u 是用戶代碼
p 是一個計劃的關(guān)閉代碼
xx 是一個主要原因代碼(小于 256 的正整數(shù))
yy 是一個次要原因代碼(小于 65536 的正整數(shù))

方法二:調(diào)用API

復(fù)制代碼 代碼如下:

private const int SE_PRIVILEGE_ENABLED = 0x00000002;
private const int TOKEN_QUERY = 0x00000008;
private const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
private const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";

[Flags]
public enum ExitWindows : uint
{
LogOff = 0x00, //注銷
ShutDown = 0x01, //關(guān)機(jī)
Reboot = 0x02, //重啟
Force = 0x04,
PowerOff = 0x08,
ForceIfHung = 0x10
}

[Flags]
private enum ShutdownReason : uint
{
MajorApplication = 0x00040000,
MajorHardware = 0x00010000,
MajorLegacyApi = 0x00070000,
MajorOperatingSystem = 0x00020000,
MajorOther = 0x00000000,
MajorPower = 0x00060000,
MajorSoftware = 0x00030000,
MajorSystem = 0x00050000,
MinorBlueScreen = 0x0000000F,
MinorCordUnplugged = 0x0000000b,
MinorDisk = 0x00000007,
MinorEnvironment = 0x0000000c,
MinorHardwareDriver = 0x0000000d,
MinorHotfix = 0x00000011,
MinorHung = 0x00000005,
MinorInstallation = 0x00000002,
MinorMaintenance = 0x00000001,
MinorMMC = 0x00000019,
MinorNetworkConnectivity = 0x00000014,
MinorNetworkCard = 0x00000009,
MinorOther = 0x00000000,
MinorOtherDriver = 0x0000000e,
MinorPowerSupply = 0x0000000a,
MinorProcessor = 0x00000008,
MinorReconfig = 0x00000004,
MinorSecurity = 0x00000013,
MinorSecurityFix = 0x00000012,
MinorSecurityFixUninstall = 0x00000018,
MinorServicePack = 0x00000010,
MinorServicePackUninstall = 0x00000016,
MinorTermSrv = 0x00000020,
MinorUnstable = 0x00000006,
MinorUpgrade = 0x00000003,
MinorWMI = 0x00000015,
FlagUserDefined = 0x40000000,
FlagPlanned = 0x80000000
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}

[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern IntPtr GetCurrentProcess();

[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
private static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);

[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);

[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
private static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);

[DllImport("user32.dll")]
private static extern bool ExitWindowsEx(ExitWindows uFlags, ShutdownReason dwReason);

/// <summary>
/// 關(guān)機(jī)、重啟、注銷windows
/// </summary>
/// <param name="flag"></param>
public static void DoExitWindows(ExitWindows flag)
{
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
ExitWindowsEx(flag, ShutdownReason.MajorOther);
}

相關(guān)文章

  • 淺談C#中的string駐留池

    淺談C#中的string駐留池

    這篇文章主要介紹了C#中的string駐留池的的相關(guān)資料,文中示例代碼非常細(xì)致,供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • C#實(shí)現(xiàn)啟動,關(guān)閉與查找進(jìn)程的方法

    C#實(shí)現(xiàn)啟動,關(guān)閉與查找進(jìn)程的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)啟動,關(guān)閉與查找進(jìn)程的方法,通過簡單實(shí)例形式分析了C#針對進(jìn)程的啟動,關(guān)閉與查找的相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • C#中方法的詳細(xì)介紹

    C#中方法的詳細(xì)介紹

    本篇文章介紹了,C#中方法的詳細(xì)說明。需要的朋友參考下
    2013-04-04
  • 如何使用C#在PDF文件添加圖片印章

    如何使用C#在PDF文件添加圖片印章

    文檔中添加印章可以起一定的作用,比如,防止文件隨意被使用,或者確保文檔內(nèi)容的安全性和權(quán)威性。C#添加圖片印章其實(shí)也有很多實(shí)現(xiàn)方法,這里我使用的是免費(fèi)的第三方軟件Free Spire.PDF,向大家闡述如何以編程的方式在PDF文件中添加圖片印章
    2017-01-01
  • C#取得隨機(jī)顏色的方法

    C#取得隨機(jī)顏色的方法

    這篇文章主要介紹了C#取得隨機(jī)顏色的方法,通過自定義函數(shù)實(shí)現(xiàn)隨機(jī)顏色的功能,非常具有實(shí)用價值,需要的朋友可以參考下
    2015-01-01
  • C#使用Socket上傳并保存圖片的方法

    C#使用Socket上傳并保存圖片的方法

    這篇文章主要介紹了C#使用Socket上傳并保存圖片的方法,涉及C#使用Socket進(jìn)行文件傳輸?shù)南嚓P(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • C#實(shí)現(xiàn)冒泡排序算法的代碼示例

    C#實(shí)現(xiàn)冒泡排序算法的代碼示例

    冒泡排序即是對數(shù)組每次輪循出最大數(shù)或最小數(shù)放在隊尾,這里我們來看一下C#實(shí)現(xiàn)冒泡排序算法的代碼示例,需要的朋友可以參考下
    2016-07-07
  • 深入委托與多播委托的詳解

    深入委托與多播委托的詳解

    本篇文章是對委托與多播委托進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • C#二分查找算法實(shí)例分析

    C#二分查找算法實(shí)例分析

    這篇文章主要介紹了C#二分查找算法,實(shí)例分析了C#二分查找的相關(guān)技巧,非常具有實(shí)用價值,需要的朋友可以參考下
    2015-04-04
  • C#中LINQ多條件JOIN時為什么可以使用匿名類

    C#中LINQ多條件JOIN時為什么可以使用匿名類

    這篇文章主要給大家介紹了關(guān)于C#中LINQ多條件JOIN時為什么可以使用匿名類的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧
    2018-09-09

最新評論