C#实现关机、重启

本程序用到的控件只有三个按钮,分别为:重启,关机,退出。

打开右边的“解决方案资源管理器”--展开树结构--引用--添加引用---在弹出的对话框中找到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(1616); 
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(11216); 
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(21616); 
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(614); 
this.ClientSize = new System.Drawing.Size(30453); 
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(); 
}
 
}
 
}

 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值