求C#让电脑重起的代码! 15
展开全部
你是需要重启远程还是本地机器?
给你讲远程的吧,本地就更简单了。。。
1.连接远程计算机:
ConnectionOptions options = new ConnectionOptions();
options.Username = "管理者帐号用户名";
options.Password = "管理者帐号口令";
ManagementScope scope = new ManagementScope("\\\\" + "远程计算机名或IP地址" + "\\root\\cimv2", options) ;
scope.Connect(); //用给定管理者用户名和口令连接远程的计算机
2.得到在远程计算机中可以进行WMI控制:
System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher query1 = new ManagementObjectSearcher(scope, oq) ;
ManagementObjectCollection queryCollection1 = query1.Get(); //得到WMI控制
调用WMI控制,实现重启远程计算机:
foreach(ManagementObject mo in queryCollection1)
{
string[] str = new string[];
mo.InvokeMethod("Reboot", str) ; //重启远程计算机
}
给你讲远程的吧,本地就更简单了。。。
1.连接远程计算机:
ConnectionOptions options = new ConnectionOptions();
options.Username = "管理者帐号用户名";
options.Password = "管理者帐号口令";
ManagementScope scope = new ManagementScope("\\\\" + "远程计算机名或IP地址" + "\\root\\cimv2", options) ;
scope.Connect(); //用给定管理者用户名和口令连接远程的计算机
2.得到在远程计算机中可以进行WMI控制:
System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher query1 = new ManagementObjectSearcher(scope, oq) ;
ManagementObjectCollection queryCollection1 = query1.Get(); //得到WMI控制
调用WMI控制,实现重启远程计算机:
foreach(ManagementObject mo in queryCollection1)
{
string[] str = new string[];
mo.InvokeMethod("Reboot", str) ; //重启远程计算机
}
展开全部
还有可以调用dos命令令电脑重启,如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ShutDown
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem3,
this.menuItem4});
this.menuItem1.Text = "文件";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "关机";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "注销";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// menuItem4
//
this.menuItem4.Index = 2;
this.menuItem4.Text = "重启";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Cursor = System.Windows.Forms.Cursors.Hand;
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "关机";
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
//p.StartInfo.Arguments = "shutdown -s -t 3";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
p.StandardInput.WriteLine("shutdown -s -t 8");
p.StandardInput.WriteLine("exit");
string s = p.StandardOutput.ReadToEnd();
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process x = new System.Diagnostics.Process();
x.StartInfo.FileName = "cmd.exe";
//p.StartInfo.Arguments = "shutdown -s -t 3";
x.StartInfo.UseShellExecute = false;
x.StartInfo.RedirectStandardInput = true;
x.StartInfo.RedirectStandardOutput = true;
x.StartInfo.RedirectStandardError = true;
x.Start();
x.StandardInput.WriteLine("shutdown -l -t 3");
x.StandardInput.WriteLine("exit");
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process q = new System.Diagnostics.Process();
q.StartInfo.FileName = "cmd.exe";
//p.StartInfo.Arguments = "shutdown -s -t 3";
q.StartInfo.UseShellExecute = false;
q.StartInfo.RedirectStandardInput = true;
q.StartInfo.RedirectStandardOutput = true;
q.StartInfo.RedirectStandardError = true;
q.Start();
q.StandardInput.WriteLine("shutdown -r -t 5");
q.StandardInput.WriteLine("exit");
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ShutDown
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem3,
this.menuItem4});
this.menuItem1.Text = "文件";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "关机";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "注销";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// menuItem4
//
this.menuItem4.Index = 2;
this.menuItem4.Text = "重启";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Cursor = System.Windows.Forms.Cursors.Hand;
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "关机";
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
//p.StartInfo.Arguments = "shutdown -s -t 3";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
p.StandardInput.WriteLine("shutdown -s -t 8");
p.StandardInput.WriteLine("exit");
string s = p.StandardOutput.ReadToEnd();
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process x = new System.Diagnostics.Process();
x.StartInfo.FileName = "cmd.exe";
//p.StartInfo.Arguments = "shutdown -s -t 3";
x.StartInfo.UseShellExecute = false;
x.StartInfo.RedirectStandardInput = true;
x.StartInfo.RedirectStandardOutput = true;
x.StartInfo.RedirectStandardError = true;
x.Start();
x.StandardInput.WriteLine("shutdown -l -t 3");
x.StandardInput.WriteLine("exit");
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process q = new System.Diagnostics.Process();
q.StartInfo.FileName = "cmd.exe";
//p.StartInfo.Arguments = "shutdown -s -t 3";
q.StartInfo.UseShellExecute = false;
q.StartInfo.RedirectStandardInput = true;
q.StartInfo.RedirectStandardOutput = true;
q.StartInfo.RedirectStandardError = true;
q.Start();
q.StandardInput.WriteLine("shutdown -r -t 5");
q.StandardInput.WriteLine("exit");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询