利用网页强制重启服务器(C#)

  点击Button1,强制重启21.gif

ContractedBlock.gif ExpandedBlockStart.gif C#重启服务器代码
None.gifusing System;
None.gif
using System.Collections;
None.gif
using System.ComponentModel;
None.gif
using System.Data;
None.gif
using System.Drawing;
None.gif
using System.Web;
None.gif
using System.Web.SessionState;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.HtmlControls;
None.gif
using System.Runtime.InteropServices; 
None.gif
namespace WebApplication1_advapi
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// WebForm1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class WebForm1 : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected const int SE_PRIVILEGE_ENABLED = 0x2
InBlock.gif        
protected const int TOKEN_QUERY = 0x8
InBlock.gif        
protected const int TOKEN_ADJUST_PRIVILEGES = 0x20
InBlock.gif        
protected const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
InBlock.gif        
protected const int EWX_LOGOFF = 0x0
InBlock.gif        
protected const int EWX_SHUTDOWN = 0x1
InBlock.gif        
protected const int EWX_REBOOT = 0x2
InBlock.gif        
protected const int EWX_FORCE = 0x4
InBlock.gif        
protected const int EWX_POWEROFF = 0x8;
InBlock.gif        
protected System.Web.UI.WebControls.Button Button1; 
InBlock.gif        
protected const int EWX_FORCEIFHUNG = 0x10
InBlock.gif
InBlock.gif
InBlock.gif        [StructLayout(LayoutKind.Sequential, Pack
=1)]
InBlock.gif            
protected struct LuidStruct 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public int Count;
InBlock.gif            
public long Luid;
InBlock.gif            
public int Attr;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [DllImport(
"kernel32.dll", ExactSpelling=true)]
InBlock.gif        
protected static extern IntPtr GetCurrentProcess();
InBlock.gif
InBlock.gif        [DllImport(
"advapi32.dll", SetLastError=true)]
InBlock.gif        
protected static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
InBlock.gif
InBlock.gif        [DllImport(
"advapi32.dll", SetLastError=true)]
InBlock.gif        
protected static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
InBlock.gif
InBlock.gif        [DllImport(
"advapi32.dll", SetLastError=true, ExactSpelling=true)]
InBlock.gif        
protected static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref LuidStruct newst, int len, IntPtr prev, IntPtr relen);
InBlock.gif
InBlock.gif        [DllImport(
"user32.dll", SetLastError=true, ExactSpelling=true)]
InBlock.gif        
protected static extern bool ExitWindowsEx(int flg, int rea);
InBlock.gif
InBlock.gif        
protected static void DoExitWindows(int flg) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            LuidStruct tp;
InBlock.gif            IntPtr hproc 
= GetCurrentProcess();
InBlock.gif            IntPtr htok 
= IntPtr.Zero;
InBlock.gif
InBlock.gif            OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES 
| TOKEN_QUERY, ref htok);
InBlock.gif            tp.Count 
= 1;
InBlock.gif            tp.Luid 
= 0;
InBlock.gif            tp.Attr 
= SE_PRIVILEGE_ENABLED;
InBlock.gif            LookupPrivilegeValue(
null, SE_SHUTDOWN_NAME, ref tp.Luid);
InBlock.gif            AdjustTokenPrivileges(htok, 
falseref tp, 0, IntPtr.Zero, IntPtr.Zero);
InBlock.gif            ExitWindowsEx(flg, 
0);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void Shutdown() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DoExitWindows(EWX_SHUTDOWN);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void Reboot() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DoExitWindows(EWX_REBOOT 
| EWX_FORCE);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void Logoff() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DoExitWindows(EWX_LOGOFF);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [DllImport(
"advapi32.dll", SetLastError=true, ExactSpelling=false)]
InBlock.gif        
protected static extern bool InitiateSystemShutdown(string name, string msg, int timeout, bool force, bool reboot);
InBlock.gif
InBlock.gif
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif

ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Button1.Click += new System.EventHandler(this.Button1_Click);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void Button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif               InitiateSystemShutdown(
null,null,0,true,true);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


本来想找重启服务资料,没想到能GOOGLE到这种代码。
原文地址:http://gmm.cnblogs.com/articles/254668.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值