菜鸟问题:请问谁有C#实现关机的代码啊? 如题!!!!谢谢!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可使用wmi,上网搜“wmi 关机”之类的,应该有.. 参看http://community.csdn.net/Expert/topicview.asp?id=1704985http://community.csdn.net/Expert/topicview.asp?id=1575229 本程序用到的控件只有三个按钮,分别为:重启,关机,退出。 打开右边的“解决方案资源管理器”--展开树结构--引用--添加引用---在弹出的对话框中找到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(); } } } 求助 sql插入语句问题 100分求教文件导出的方法: 一个关于正则表达式的问题 SqlDatAadapter中insertcommand批量插入数据不成功 关于.Net程序调试中"编辑与继续"功能的探讨(请高手帮忙,新手学习) C# WINFORM 中如何改变窗体大小?? 一个读INI文件的问题? c#能做activeX吗? 国庆回家带点什么呢? web下的绘图? 请问如何实本地文件与服务器文件的对比? 怎么将dataGrid中选中的数据在对应的文体框中显示出来
http://community.csdn.net/Expert/topicview.asp?id=1704985
http://community.csdn.net/Expert/topicview.asp?id=1575229
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();
}
}
}