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

开发 后端
本文列出了ASP.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关机代码。

【编辑推荐】

  1. ASP.NET DetailsView中显示选中产品的详细信息
  2. ASP.NET 2.0数据教程:GridView选择行
  3. ASP.NET 2.0数据教程:GridView显示数据
  4. ASP.NET 2.0中添加GridView到页面
  5. 新增ASP.NET页面时的注意事项
责任编辑:book05 来源: cnblogs
相关推荐

2009-08-04 17:16:16

ASP.NET代码优化

2012-05-07 14:50:32

ASP.NET

2009-07-22 17:45:35

ASP.NET教程

2009-07-28 17:17:19

ASP.NET概述

2009-08-03 14:22:33

什么是ASP.NET

2009-08-04 11:29:14

HTML代码ASP.NET控件

2009-08-10 17:17:10

ASP.NET安装部署

2009-02-10 10:07:48

ASP.NETContextMenuVisual Stud

2009-07-29 15:51:29

ASP.NET中执行w

2009-07-27 12:22:03

ASP.NET和ASPASP.NET入门教程

2009-07-28 16:31:03

Windows 200

2009-08-05 16:59:55

ASP.NET组件设计

2009-07-30 14:26:30

ASP.NET常用代码

2009-07-22 17:35:23

代码隐藏文件ASP.NET

2009-07-30 14:32:18

ASP.NET常用代码

2009-04-02 11:00:09

微软ASP.NETMVC

2009-08-10 13:32:15

ASP.NET TimASP.NET组件设计

2009-07-29 17:11:25

ASP.NET ISA

2009-07-29 16:08:07

ASP和ASP.NET

2009-08-03 13:38:18

ASP.NET编程模型
点赞
收藏

51CTO技术栈公众号