C#中實現關機程式

來源:互聯網
上載者:User
本程式用到的控制項只有三個按鈕,分別為:重啟,關機,退出。

開啟右邊的“方案總管”--展開樹結構--引用--添加引用---在彈出的對話方塊中找到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();
}
}
}
---------------------

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.