实现XP系统的开关机???

WU-ZhiLe 2009-03-02 11:00:10
用c#代码怎么实现XP系统的开关机???
...全文
185 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
yeah86 2009-03-04
  • 打赏
  • 举报
回复
线程调用
WU-ZhiLe 2009-03-04
  • 打赏
  • 举报
回复
关机可以用这个,
System.diagnostics.process=new system.diagnostics.process();
myprocess.startinfo.filename="cmd.exe";
myprocess.startinfo.useshellexecute=false;
myprocess.startinfo.Redirectstandardlnput=true;
myprocess.startinfo.Redirectstandardoutput=true;
myprocess.startinfo.RedirectstandardError=true;
myprocess.startinfo.Createnowindow=true;
myprocess.start();
myprocess.standardlnput.writeline("shoutdown-s-t 0");


system.diagnostics.process.start("cmd.exe","shutdown-s-t%time%");
-s关机
-l注销
-r重启
-t+时间
shutdown+你要干什么+-t你要执行的时间

a12321321321312321 2009-03-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jinjazz 的回复:]
如果有权限的话,直接用rundll32.exe调用系统命令就可以了
http://blog.csdn.net/jinjazz/archive/2008/04/17/2302095.aspx

如果没有权限需要先用windows api函数提升关机权限
[/Quote]
顶~
  • 打赏
  • 举报
回复
csdn 下载的是
liuyang052 2009-03-04
  • 打赏
  • 举报
回复
调用API。网上搜
zhaozhijun0207 2009-03-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jijunwu 的回复:]
需要调用API
[/Quote]

定时关机怎么做?
iGouzy 2009-03-04
  • 打赏
  • 举报
回复
Process 一个 shutdown -s -t 0 就行了~~

开机呀楼主~~这个没想过,关机以后恐怕 .Net Runtime 就没了,要实现开机的话,起码需要一台服务器,做网络唤醒才行
jhdxhj 2009-03-04
  • 打赏
  • 举报
回复
jhdxhj 2009-03-04
  • 打赏
  • 举报
回复
顶哦
天乐 2009-03-04
  • 打赏
  • 举报
回复
关机很简单。

定时开机怎么办?
WU-ZhiLe 2009-03-04
  • 打赏
  • 举报
回复
thank
null1 2009-03-03
  • 打赏
  • 举报
回复
http://blog.csdn.net/null1/archive/2008/11/03/3208145.aspx
aimeast 2009-03-02
  • 打赏
  • 举报
回复
学习!
jinjazz 2009-03-02
  • 打赏
  • 举报
回复
如果有权限的话,直接用rundll32.exe调用系统命令就可以了
http://blog.csdn.net/jinjazz/archive/2008/04/17/2302095.aspx

如果没有权限需要先用windows api函数提升关机权限
  • 打赏
  • 举报
回复
调用API的部分直接复制
  • 打赏
  • 举报
回复

using System.Runtime.InteropServices;//用来调用API函数
using System.Management;//调用API
//==============**********调用API*************
[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 flg, 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 bool DoExitWin(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);
return ok;
}
/// <summary>
/// 注销计算机
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button47_Click(object sender, System.EventArgs e)
{
DoExitWin(EWX_LOGOFF);
}
/// <summary>
/// 关闭计算机
/// </summary>
private void button46_Click(object sender, System.EventArgs e)
{
DoExitWin(EWX_SHUTDOWN);
}

/// <summary>
/// 取消操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button43_Click(object sender, System.EventArgs e)
{
this.Close();
}

/// <summary>
/// 重启计算机
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button45_Click(object sender, System.EventArgs e)
{
DoExitWin(EWX_REBOOT);
}
  • 打赏
  • 举报
回复
需要调用API

111,126

社区成员

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

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

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