ASP.NET关机代码(Windows为本机)


  本文标签:.NET关机代码

  编写.NET关机代码,首先导入这个命名空间using System.Runtime.InteropServices;

  这个是关闭本机的代码,比如说程序是放在服务器上,那么关闭的是Server,而不是客户端  。

  以下.NET关机代码针对windows

  1. using System;    
  2. using System.Data;    
  3. using System.Configuration;    
  4. using System.Web;    
  5. using System.Web.Security;    
  6. using System.Web.UI;    
  7. using System.Web.UI.WebControls;    
  8. using System.Web.UI.WebControls.WebParts;    
  9. using System.Web.UI.HtmlControls;    
  10. using System.Runtime.InteropServices;    
  11.  
  12. public partial class _Default : System.Web.UI.Page     
  13.  
  14. {    
  15.     protected void Page_Load(object sender, EventArgs e)    
  16.     {    
  17.         DoExitWin(EWX_SHUTDOWN);    
  18.     }    
  19.     [StructLayout(LayoutKind.Sequential, Pack = 1)]    
  20.     internal struct TokPriv1Luid    
  21.     {    
  22.         public int Count;    
  23.         public long Luid;    
  24.         public int Attr;    
  25.     }    
  26.    
  27. [DllImport("kernel32.dll", ExactSpelling = true)]    
  28.    internal static extern IntPtr GetCurrentProcess();    
  29.    
  30.    [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]    
  31.    internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);    
  32.    
  33.    [DllImport("advapi32.dll", SetLastError = true)]    
  34.    internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);    
  35.    
  36.    [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]    
  37.    internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,    
  38.    ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);    
  39.    
  40.    [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]    
  41.    internal static extern bool ExitWindowsEx(int flg, int rea);    
  42.  
  43. internal const int SE_PRIVILEGE_ENABLED = 0x00000002;    
  44.    internal const int TOKEN_QUERY = 0x00000008;    
  45.    internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;    
  46.    internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";    
  47.    internal const int EWX_LOGOFF = 0x00000000;    
  48.    internal const int EWX_SHUTDOWN = 0x00000001;    
  49.    internal const int EWX_REBOOT = 0x00000002;    
  50.    internal const int EWX_FORCE = 0x00000004;    
  51.    internal const int EWX_POWEROFF = 0x00000008;    
  52.    internal const int EWX_FORCEIFHUNG = 0x00000010;    
  53.  
  54. private static void DoExitWin(int flg)    
  55.     {    
  56.         bool ok;    
  57.         TokPriv1Luid tp;    
  58.         IntPtr hproc = GetCurrentProcess();    
  59.         IntPtr htok = IntPtr.Zero;    
  60.         ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);    
  61.         tp.Count = 1;    
  62.         tp.Luid = 0;    
  63.         tp.Attr = SE_PRIVILEGE_ENABLED;    
  64.         ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);    
  65.         ok = AdjustTokenPrivileges(htok, falseref tp, 0, IntPtr.Zero, IntPtr.Zero);    
  66.         ok = ExitWindowsEx(flg, 0);    
  67.     }    
  68. }    
  69.  

  以上就是.NET关机代码  。