路过的近来看看!

PY_0516 2006-10-31 11:43:26
本人想求可以关闭或者重起电脑的C#代码! 请知道的附代码!
有分相送!~
...全文
81 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
PY_0516 2006-11-01
  • 打赏
  • 举报
回复
哦 没有看清楚 不好意思 知道了
PY_0516 2006-11-01
  • 打赏
  • 举报
回复
再问一下 兄弟! using System.Management;
没有此命名空间 这是怎么回事 帮忙说下 谢谢!~
liujia_0421 2006-11-01
  • 打赏
  • 举报
回复
学习,收藏一下...
oolongTea 2006-11-01
  • 打赏
  • 举报
回复
回复人: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 extern
private static extern int ExitWindowsEx(int x,int y);

//这个是退出按钮的实现,即实现程序的退出
private void Exit_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
}
}

110,549

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧