路过的近来看看! 本人想求可以关闭或者重起电脑的C#代码! 请知道的附代码!有分相送!~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 回复人:jishuzhichi(技术支持) ( 一级(初级)) 信誉:100 2006-9-28 9:49:32 得分:20? 本程序用到的控件只有三个按钮,分别为:重启,关机,退出。打开右边的“解决方案资源管理器”--展开树结构--引用--添加引用---在弹出的对话框中找到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 externprivate static extern int ExitWindowsEx(int x,int y);//这个是退出按钮的实现,即实现程序的退出private void Exit_Click(object sender, System.EventArgs e){Application.Exit();}}} 再问一下 兄弟! using System.Management;没有此命名空间 这是怎么回事 帮忙说下 谢谢!~ 一段不能理解的代码,求高人解答 请问,DataSet中使用外键约束 C# 2005 里如何在datagridview中做chckbox是否被选中 MDI同时打开两个子窗口的问题 (WinForm)DataGrid的ColumnHeader的高度可以设置吗? WPF,请问初始化文本 菜鸟问:C#中怎样实现柱状图和报表 怎么样判断一个字符串中是否含有空格?以及那里有空格? 请问如何随机产生两随机数值 有谁知道怎么获取CPU和内存当前的使用情况? c#中如何创建XMLHTTP对像 如何处理POST过来的XML数据?
? 本程序用到的控件只有三个按钮,分别为:重启,关机,退出。打开右边的“解决方案资源管理器”--展开树结构--引用--添加引用---在弹出的对话框中找到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();
}
}
}
没有此命名空间 这是怎么回事 帮忙说下 谢谢!~