| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 719 人关注过本帖
标题:如何实现关机功能
只看楼主 加入收藏
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
收藏
 问题点数:0 回复次数:4 
如何实现关机功能
如何用c#写一段程序,让它来关闭计算机?
搜索更多相关主题的帖子: 计算机 
2007-05-17 23:40
飙马
Rank: 5Rank: 5
来 自:马里亚纳
等 级:贵宾
威 望:15
帖 子:779
专家分:280
注 册:2007-3-28
收藏
得分:0 

using System;
using System.Runtime.InteropServices;

class shoutdown{
[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 static void 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);
}

public static void Main()
{
Console.WriteLine("正在关闭计算机……");
// 修改 EWX_SHUTDOWN 或者 EWX_LOGOFF, EWX_REBOOT等实现不同得功能。
// 在XP下可以看到帮助信息,以得到不同得参数
// SHUTDOWN /?
DoExitWin(EWX_SHUTDOWN);
}
}


IT精英如同彩票:平凡的人像5块也中不到一样普遍,努力一点你中了5元保了个本。奖金越高,机率也就越小,付出的也越多,盖茨如同500万一样稀有。虽然每天忙碌而平凡,但我努力成为精英,做梦中了500万。
2007-05-18 08:51
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
收藏
得分:0 
回复:(飙马)
谢谢版主...

帮助那些真正需要帮助的人,是对帮助你的人最好的回报!
2007-06-13 23:13
飙马
Rank: 5Rank: 5
来 自:马里亚纳
等 级:贵宾
威 望:15
帖 子:779
专家分:280
注 册:2007-3-28
收藏
得分:0 
以前的帖子——《c#经典小程序》中有,大家可以找找!

IT精英如同彩票:平凡的人像5块也中不到一样普遍,努力一点你中了5元保了个本。奖金越高,机率也就越小,付出的也越多,盖茨如同500万一样稀有。虽然每天忙碌而平凡,但我努力成为精英,做梦中了500万。
2007-06-14 09:00
随风云
Rank: 1
等 级:新手上路
威 望:1
帖 子:263
专家分:0
注 册:2007-6-28
收藏
得分:0 

刚好我今天用C#写了一个,我是调用的cmd.exe应该还行:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics ;

namespace Point
{
/// <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()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";

this.Load += new System.EventHandler(this.Form1_Load);

}
#endregion

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

private void Form1_Load(object sender,System.EventArgs e)
{
Process p=new Process();
p.StartInfo .FileName ="cmd.exe";
p.StartInfo .UseShellExecute =false;
p.StartInfo .RedirectStandardInput =true;
p.StartInfo .RedirectStandardOutput =true;
p.StartInfo.RedirectStandardError =true;
p.StartInfo .CreateNoWindow =false;
p.Start ();
p.StandardInput.WriteLine("shutdown -s -f -t 0");
p.StandardInput.WriteLine("exit");

string strRst = p.StandardOutput.ReadToEnd();


Label l=new Label ();
Controls.Add (l);
l.Text =strRst;
l.Size=new System.Drawing .Size (this.Size .Width ,this.Size .Height );


}

}
}


真的想象风一样去流浪!
2007-07-03 18:52
快速回复:如何实现关机功能
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012957 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved