关机,重启,注销程序

sunchen89129 2010-04-23 09:28:11
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace API测试
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

//退出
private void button4_Click(object sender, EventArgs e)
{
Application.Exit();
}

const byte LOGOFF = 0; //
const byte SHUTDOWN = 1;
const byte REBOOT = 2;

[DllImportAttribute("User32.dll")]
static extern long ExitWindowsEx(byte uFlags, uint dwReason);

//关机
private void button3_Click(object sender, EventArgs e)
{
ExitWindowsEx(SHUTDOWN, 0);
}

//重启
private void button2_Click(object sender, EventArgs e)
{
ExitWindowsEx(REBOOT, 0);
}

//注销
private void button1_Click(object sender, EventArgs e)
{
ExitWindowsEx(LOGOFF, 0);
}
}
}


我这样写只有注销的功能实现了,而关机和重启这两个功能不能实现,请高手指教,能给出详细的代码吗,小弟非常感谢,我是C#的初学

者。如有什么好的资料或程序能否传到我的邮箱:sunfu4359@163.com,QQ:402200280,本人非常想学好C#,不知从那方面,怎么去提高自

己的水平。若有高人指点就更好了!!!
...全文
169 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
a82344626 2010-04-24
  • 打赏
  • 举报
回复

public class ServerRoot
{
[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 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);
}
}

rjianfeili 2010-04-24
  • 打赏
  • 举报
回复
Process.Start("Shutdown.exe", " -s -t 0");//关机
Process.Start("shutdown.exe", " -r -t 0");//重启
geminizane 2010-04-24
  • 打赏
  • 举报
回复
哈哈...楼上好风趣...
njyang110 2010-04-24
  • 打赏
  • 举报
回复
百度一下,例子半年看不完
风之影子 2010-04-24
  • 打赏
  • 举报
回复

///命名空间
///using System.Runtime.InteropServices;
///using System.Diagnostics;
#region 注销、关闭、重启计算机
[DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)]
//ExitWindowsEx 函数
private static extern int ExitWindowsEx(int uFlags, int dwReserved);
public void zhuxiao() //注销
{
ExitWindowsEx(0, 0);
}

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);
}
}
#endregion


wuyq11 2010-04-23
  • 打赏
  • 举报
回复
System.Diagnostics.Process.Start("shutdown.exe","-s -t 0");
EWX_SHUTDONW,EWX_POWEROFF需要关机特权。
默认是不包含这些特权的

110,571

社区成员

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

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

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