C# 如何实现关机重启

xinloveminnie 2009-07-30 11:28:27
想做个定时关机重启的窗体程序 对关机重启的实现 网上找了下 说:使用C#调用cmd.exe执行shutdown.exe命令就行。

可行吗?可以的话 是怎么调用的? 还有其它简单的方法吗 麻烦进贴的各位了
...全文
1175 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
JasomH 2012-08-02
  • 打赏
  • 举报
回复
我这边测试了一下,为甚么注销后应用程序就跟着关闭了?
络大大 2012-02-05
  • 打赏
  • 举报
回复
真要赞一下啊,高手如云。
bluedodo 2011-03-19
  • 打赏
  • 举报
回复
标记7楼
LJ_liujue 2011-03-19
  • 打赏
  • 举报
回复
我晕!! 一个比一个麻烦 头都晕了
sishenfeimeng 2010-12-13
  • 打赏
  • 举报
回复
方法挺多的!不错啊...
cannycsy 2010-12-13
  • 打赏
  • 举报
回复
留个脚印
loyy77 2009-11-29
  • 打赏
  • 举报
回复
我还是先用dos实现吧。至少现在能说出个道理来。hoho
API还没接触过。
daimin_1 2009-11-12
  • 打赏
  • 举报
回复
API调用会出现“当前内存地址不可写”的错误,导致系统蓝屏,是不是我的D版系统有问题啊?
fishia 2009-08-01
  • 打赏
  • 举报
回复
学习 学习
Jack_Senlan 2009-07-31
  • 打赏
  • 举报
回复

好强,学习
十八道胡同 2009-07-31
  • 打赏
  • 举报
回复
 [StructLayout(LayoutKind.Sequential, Pack = 1)] 
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}

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

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

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

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

[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool ExitWindowsEx(int DoFlag, int rea);

internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010;

private static void DoExitWin(int DoFlag)
{
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(DoFlag, 0);
}

public static void Reboot()
{
DoExitWin(EWX_FORCE | EWX_REBOOT);
}

public static void PowerOff()
{
DoExitWin(EWX_FORCE | EWX_POWEROFF);
}

public static void LogOff()
{
DoExitWin(EWX_FORCE | EWX_LOGOFF);
}
}
wuyq11 2009-07-31
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace A
{
public class Shudown
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}

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

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

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

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

[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool ExitWindowsEx(int DoFlag, int rea);

internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010;

private static void DoExitWin(int DoFlag)
{
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(DoFlag, 0);
}

public static void Reboot()
{
DoExitWin(EWX_FORCE | EWX_REBOOT);
}

public static void PowerOff()
{
DoExitWin(EWX_FORCE | EWX_POWEROFF);
}

public static void LogOff()
{
DoExitWin(EWX_FORCE | EWX_LOGOFF);
}
}
}
kxtm01 2009-07-31
  • 打赏
  • 举报
回复 1
我给你这两个方法copy走吧,拿去粘贴在你的项目里就能用~~~记得调用这两个方法哦~~~


//------------关机方法
public void guanji()
{
try
{
//启动本地程序并执行命令
Process.Start("Shutdown.exe", " -s -t 0");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//----------重启重启
public void chongqi()
{
try
{
//启动本地程序并执行命令
Process.Start("shutdown.exe"," -r -t 0");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

//赠送一个使用win32API函数注销的

//===================================================================================注销 函数 声明
[DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)]
//ExitWindowsEx 函数
private static extern int ExitWindowsEx(int uFlags, int dwReserved);
//======================================================================================
public void zhuxiao() //注销
{
ExitWindowsEx(0, 0);
}
y82907966 2009-07-31
  • 打赏
  • 举报
回复

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 24);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "关机";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(112, 24);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "注销";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(208, 24);
this.button3.Name = "button3";
this.button3.TabIndex = 2;
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(292, 70);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

#region 微软提供的关机接口 调用系统的 kernel32.dll advapi32.dll user32.dll 实现的关机
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}

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

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

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

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

[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool ExitWindowsEx(int DoFlag, int rea);

internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
internal const int EWX_FORCEIFHUNG = 0x00000010;

private static bool DoExitWin(int DoFlag)
{
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(DoFlag, 0);
return ok;
}

/**//// <summary>
/// 重新启动
/// </summary>
public static bool Reboot()
{
return DoExitWin(EWX_FORCE | EWX_REBOOT);
}

/**//// <summary>
/// 关机
/// </summary>
public static bool PowerOff()
{
return DoExitWin(EWX_FORCE | EWX_POWEROFF);
}

/**//// <summary>
/// 注销
/// </summary>
public static bool LogOff()
{
return DoExitWin(EWX_FORCE | EWX_LOGOFF);
}
#endregion

private void button2_Click(object sender, System.EventArgs e)
{
LogOff();
}

private void button1_Click(object sender, System.EventArgs e)
{
PowerOff();
}

private void button3_Click(object sender, System.EventArgs e)
{
Reboot();
}
}
}



VistaKobe 2009-07-31
  • 打赏
  • 举报
回复
收!
LQknife 2009-07-31
  • 打赏
  • 举报
回复
application.restart?
jking1989 2009-07-31
  • 打赏
  • 举报
回复
顶一个
jsnjlhb 2009-07-31
  • 打赏
  • 举报
回复
七楼的方法还是相当简单的,
学习学习
xinloveminnie 2009-07-31
  • 打赏
  • 举报
回复 1
[Quote=引用 7 楼 kxtm01 的回复:]
我给你这两个方法copy走吧,拿去粘贴在你的项目里就能用~~~记得调用这两个方法哦~~~

C# code//------------关机方法publicvoid guanji()
{try
{//启动本地程序并执行命令 Process.Start("Shutdown.exe"," -s -t 0");
}catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}//----------重启重启publicvoid chongqi()
{try
{//启动本地程序并执行命令 Process.Start("shutdown.exe"," -r -t 0");
}catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}//赠送一个使用win32API函数注销的//===================================================================================注销 函数 声明 [DllImport("user32.dll", EntryPoint="ExitWindowsEx", CharSet= CharSet.Ansi)]//ExitWindowsEx 函数privatestaticexternint ExitWindowsEx(int uFlags,int dwReserved);//======================================================================================publicvoid zhuxiao()//注销 {
ExitWindowsEx(0,0);
}
[/Quote]

自己找到了它的System.Diagnostics 命名空间 感谢这位兄弟了 !
kale_yun 2009-07-31
  • 打赏
  • 举报
回复
楼上的方法挺好的,我用过了
加载更多回复(13)

110,570

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧