打开Visual Studio.NET2003--文件---新建---左边选C#,右边选Window应用程序,给你的程序随便写个名--确定,这时系统已生成一个window应用程序的框架,只需把代码加进去就可以了。
本程序用到的控件只有三个按钮,分别为:重启,关机,退出。
打开右边的“解决方案资源管理器”--展开树结构--引用--添加引用---在弹出的对话框中找到System.Management---双击添加它---确定,即建成对System.Management.dll的引用。然后在程序的开头再写入
using System.Management;
using System.Runtime.InteropServices; //加入此名字间是为了引用Windows API来实现关机
程序如下:
-------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Management;
using System.Runtime.InteropServices; //加入此名字间是为了引用Windows API来实现关机
namespace RebootComputer
{
public class Form1 : System.Windows.Forms.Form
{
//---------------这部分都是控件--------
private System.Windows.Forms.Button Reboot;
private System.Windows.Forms.Button ShutDown;
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button Exit;
//---------------------------------------------------------
private ManagementObjectSearcher obj ;//关于这个类,请看上次的简简单单获取系统资源信息(共享、硬盘、网卡等)http://itbbs.pconline.com.cn/traditional/article.jsp?topic=1540826
//-----------------程序初始化----------------
public Form1()
{
InitializeComponent();
}
//-----------------------程序释放----------------------
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
//-------------------------初始化----------------------
private void InitializeComponent()
{
this.Reboot = new System.Windows.Forms.Button();
this.ShutDown = new System.Windows.Forms.Button();
this.Exit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// Reboot
//
this.Reboot.Location = new System.Drawing.Point(16, 16);
this.Reboot.Name = "Reboot";
this.Reboot.TabIndex = 0;
this.Reboot.Text = "重启";
this.Reboot.Click += new System.EventHandler(this.Reboot_Click);
//
// ShutDown
//
this.ShutDown.Location = new System.Drawing.Point(112, 16);
this.ShutDown.Name = "ShutDown";
this.ShutDown.TabIndex = 1;
this.ShutDown.Text = "关机";
this.ShutDown.Click += new System.EventHandler(this.ShutDown_Click);
//
// Exit
//
this.Exit.Location = new System.Drawing.Point(216, 16);
this.Exit.Name = "Exit";
this.Exit.TabIndex = 2;
this.Exit.Text = "退出";
this.Exit.Click += new System.EventHandler(this.Exit_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(304, 53);
this.Controls.Add(this.Exit);
this.Controls.Add(this.ShutDown);
this.Controls.Add(this.Reboot);
this.Name = "Form1";
this.Text = "重启、关机";
this.ResumeLayout(false);
}
//-----------------主函数,没有此函数可运行不了哦----------
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//--------------------重启部分程序,主要还是利用上次的简简单单获取系统资源信息(共享、硬盘、网卡等)----------------
private void Reboot_Click(object sender, System.EventArgs e)
{
obj = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
foreach(ManagementObject oc in obj.Get())
{
DialogResult result = MessageBox.Show("Are you sure want to reboot?","Confirm",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);//这里为确定用户是否点击“确定”按钮
if(DialogResult.OK == result)
{
oc.InvokeMethod("Reboot",null);//点击“确定”按钮后就调用此方法重启机器
}
}
}
//---------------关机部分程序------------------------
private void ShutDown_Click(object sender, System.EventArgs e)
{
if(DialogResult.OK == MessageBox.Show("Are you sure want to Shut Down?","Confirm",MessageBoxButtons.OKCancel,MessageBoxIcon.Information)) //同上
{//主要调用系统API来实现
ExitWindowsEx(1,0);
}
}
//这里的[DllImport("user32.dll")]是.net里属性的表达方法,DllImport为引用系统API所在的库"user32.dll"
[DllImport("user32.dll")]
//主要API是这个,注意:必须声明为static extern
private static extern int ExitWindowsEx(int x,int y);
//这个是退出按钮的实现,即实现程序的退出
private void Exit_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
}
}
---------------------
初学者不要看代码很长,其实很简单,关键的只有Reboot_Click和ShutDown_Click这两个方法,其它都是控件的布局之类的
你可以命令行调用shutdown给点儿参数就行了,具体的你搜搜这个exe