运行结果截图:
Shutdown.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace Shutdown
{
public partial class Shutdown : Form
{
public Shutdown()
{
InitializeComponent();
}
#region 按钮点击事件处理
/// <summary>
/// 关机
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPowerOff_Click(object sender, EventArgs e)
{
ExecuteShutdown("-s");
}
/// <summary>
/// 重启
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnReboot_Click(object sender, EventArgs e)
{
ExecuteShutdown("-r");
}
/// <summary>
/// 注销
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLogout_Click(object sender, EventArgs e)
{
ExecuteShutdown("-l");
}
/// <summary>
/// 执行Shutdown命令
/// </summary>
/// <param name="cmdText">命令开关</param>
public void ExecuteShutdown(string cmdText)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
//重定向标准输入输出流,必须不使用Shell
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine("shutdown " + cmdText);
p.StandardInput.WriteLine("exit");
}
#endregion
#region 窗体事件
/// <summary>
/// 窗体加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
btnLogout.BackgroundImageLayout = ImageLayout.Stretch;
btnPowerOff.BackgroundImageLayout = ImageLayout.Stretch;
btnReboot.BackgroundImageLayout = ImageLayout.Stretch;
btnLogout.Text = string.Empty;
btnPowerOff.Text = string.Empty;
btnReboot.Text = string.Empty;
}
#endregion
}
}
Shutdown.Designer.cs:
namespace Shutdown
{
partial class Shutdown
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnPowerOff = new System.Windows.Forms.Button();
this.btnReboot = new System.Windows.Forms.Button();
this.btnLogout = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnPowerOff
//
this.btnPowerOff.BackgroundImage = global::Shutdown.Properties.Resources.Shutdown;
this.btnPowerOff.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.btnPowerOff.Location = new System.Drawing.Point(12, 12);
this.btnPowerOff.Name = "btnPowerOff";
this.btnPowerOff.Size = new System.Drawing.Size(75, 75);
this.btnPowerOff.TabIndex = 0;
this.btnPowerOff.Text = "关机";
this.btnPowerOff.UseVisualStyleBackColor = true;
this.btnPowerOff.Click += new System.EventHandler(this.btnPowerOff_Click);
//
// btnReboot
//
this.btnReboot.BackgroundImage = global::Shutdown.Properties.Resources.Restart;
this.btnReboot.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.btnReboot.Location = new System.Drawing.Point(111, 12);
this.btnReboot.Name = "btnReboot";
this.btnReboot.Size = new System.Drawing.Size(75, 75);
this.btnReboot.TabIndex = 1;
this.btnReboot.Text = "重启";
this.btnReboot.UseVisualStyleBackColor = true;
this.btnReboot.Click += new System.EventHandler(this.btnReboot_Click);
//
// btnLogout
//
this.btnLogout.BackgroundImage = global::Shutdown.Properties.Resources.Logoff;
this.btnLogout.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.btnLogout.Location = new System.Drawing.Point(210, 12);
this.btnLogout.Name = "btnLogout";
this.btnLogout.Size = new System.Drawing.Size(75, 75);
this.btnLogout.TabIndex = 2;
this.btnLogout.Text = "注销";
this.btnLogout.UseVisualStyleBackColor = true;
this.btnLogout.Click += new System.EventHandler(this.btnLogout_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(299, 100);
this.Controls.Add(this.btnLogout);
this.Controls.Add(this.btnReboot);
this.Controls.Add(this.btnPowerOff);
this.Name = "Form1";
this.Text = "shutdown";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button btnPowerOff;
private System.Windows.Forms.Button btnReboot;
private System.Windows.Forms.Button btnLogout;
}
}