C#实现关机重启及注销实例代码
程序员文章站
2023-12-18 16:28:46
本文所述的c#实例代码是一个基于visual c#2010编写的关机、重启、注销程序,类似的c#程序网上还有不少,每个人的写法都各有不同,这段程序供新手参考下。本代码中还用...
本文所述的c#实例代码是一个基于visual c#2010编写的关机、重启、注销程序,类似的c#程序网上还有不少,每个人的写法都各有不同,这段程序供新手参考下。本代码中还用到了一个计时器,用来显示系统运行的时间,以下为完整实例代码:
using system; using system.drawing; using system.collections; using system.componentmodel; using system.windows.forms; using system.data; using system.runtime.interopservices; using system.threading; namespace shutdowncodes { public class form1 : system.windows.forms.form { private system.windows.forms.groupbox groupbox1; private system.windows.forms.textbox textbox1; private system.windows.forms.button button1; private system.windows.forms.button button2; private system.windows.forms.button button3; private system.componentmodel.icontainer components; [structlayout(layoutkind.sequential, pack=1)] public struct tokpriv1luid { public int count; public long luid; public int attr; } [dllimport("kernel32.dll", exactspelling=true)] // getcurrentprocess函数返回当前进程的一个句柄 public static extern intptr getcurrentprocess(); [dllimport("advapi32.dll", exactspelling=true, setlasterror=true)] // openprocesstoken函数打开一个进程的访问代号 public static extern bool openprocesstoken(intptr processhandles, int desiredaccess, ref intptr tokenhandle); [dllimport("advapi32.dll", setlasterror=true)] // lookupprivilegevalue函数获得本地唯一的标示符(luid),用于在特定的系统中 // 表示特定的优先权。 public static extern bool lookupprivilegevalue(string lpsystemname, string lpname, ref long lpluid); // adjusttokenprivileges函数允许或者禁用指定访问记号的优先权。 // 允许或者禁用优先权需要token_adjust_privileges访问权限。 [dllimport("advapi32.dll", exactspelling=true, setlasterror=true)] public static extern bool adjusttokenprivileges(intptr tokenhandle, bool disableallprivileges, ref tokpriv1luid newstate, int bufferlength, intptr previousstate,intptr returnlength); // exitwindowsex函数可以注销,关机或者重新启动系统 [dllimport("user32.dll", exactspelling=true, setlasterror=true)] public static extern bool exitwindowsex(int flg, int rea); private system.threading.timer timer; 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"; private const int ewx_logoff = 0x00000000; // 注销 private const int ewx_shutdown = 0x00000001; // 关机 private const int ewx_reboot = 0x00000002; // 重启 private const int ewx_force = 0x00000004; private static void rebootcommand(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); } public form1() { initializecomponent(); } protected override void dispose( bool disposing ) { if( disposing ) { if (components != null) { components.dispose(); } } base.dispose( disposing ); } #region windows 窗体设计器生成的代码 private void initializecomponent() { this.groupbox1 = new system.windows.forms.groupbox(); this.textbox1 = new system.windows.forms.textbox(); this.button1 = new system.windows.forms.button(); this.button2 = new system.windows.forms.button(); this.button3 = new system.windows.forms.button(); this.groupbox1.suspendlayout(); this.suspendlayout(); // // groupbox1 // this.groupbox1.controls.add(this.textbox1); this.groupbox1.location = new system.drawing.point(48, 24); this.groupbox1.name = "groupbox1"; this.groupbox1.size = new system.drawing.size(192, 56); this.groupbox1.tabindex = 0; this.groupbox1.tabstop = false; this.groupbox1.text = "系统已运行时间"; // // textbox1 // this.textbox1.location = new system.drawing.point(24, 24); this.textbox1.name = "textbox1"; this.textbox1.readonly = true; this.textbox1.size = new system.drawing.size(152, 21); this.textbox1.tabindex = 1; this.textbox1.text = ""; // // button1 // this.button1.location = new system.drawing.point(16, 112); this.button1.name = "button1"; this.button1.size = new system.drawing.size(64, 32); this.button1.tabindex = 1; this.button1.text = "关闭系统"; this.button1.click += new system.eventhandler(this.button1_click); // // button2 // this.button2.location = new system.drawing.point(112, 112); this.button2.name = "button2"; this.button2.size = new system.drawing.size(64, 32); this.button2.tabindex = 2; this.button2.text = "注销系统"; this.button2.click += new system.eventhandler(this.button2_click); // // button3 // this.button3.location = new system.drawing.point(208, 112); this.button3.name = "button3"; this.button3.size = new system.drawing.size(64, 32); this.button3.tabindex = 3; this.button3.text = "重启系统"; this.button3.click += new system.eventhandler(this.button3_click); // // form1 // this.autoscalebasesize = new system.drawing.size(6, 14); this.clientsize = new system.drawing.size(296, 173); this.controls.add(this.button3); this.controls.add(this.button2); this.controls.add(this.button1); this.controls.add(this.groupbox1); this.name = "form1"; this.text = "简单的关机程序"; this.load += new system.eventhandler(this.form1_load); this.groupbox1.resumelayout(false); this.resumelayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [stathread] static void main() { application.run(new form1()); } private void button1_click(object sender, system.eventargs e) { rebootcommand(ewx_shutdown + ewx_force); } private void button2_click(object sender, system.eventargs e) { rebootcommand(ewx_logoff + ewx_force); } private void button3_click(object sender, system.eventargs e) { rebootcommand(ewx_reboot + ewx_force); } private void form1_load(object sender, system.eventargs e) { textbox1.text = (environment.tickcount / (1000 * 60)).tostring() + "分钟"; // 每1秒钟更新一次textbox上显示的系统运行时间 timer = new system.threading.timer(new timercallback(ontimer), null, 0, 1000); } // 获得系统已运行的时间 private void ontimer(object state) { textbox1.text = (environment.tickcount / (1000 * 60)).tostring() + "分钟"; textbox1.refresh(); } } }