.net下编程远程启动服务
来源:互联网 发布:matlab数据分析方法 编辑:程序博客网 时间:2024/05/22 00:36
*********************************************************************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ServiceProcess;
using System.Management;
namespace ControlServer
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
private string strPath;
private ManagementClass managementClass;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
if( RemoteConnectValidate("****,****,****,****","***","***") )
{
Win32ServiceManager("****,****,****,****","***","***");
// IP, 用户名 密码
string [,] services = GetServiceList();
for(int i= 0;i<services.GetLength(0);i++)
{
Response.Write(services[i,0]);
Response.Write(services[i,1]);
Response.Write(services[i,2]);
Response.Write(services[i,3]);
Response.Write("<br>");
}
string strRst = null;
string serviceName = "IISADMIN";
ManagementObject mo = this.managementClass.CreateInstance();
mo.Path = new ManagementPath(this.strPath+".Name=/""+serviceName+"/"");
mo.InvokeMethod("StartService",null);
//StartService 启动服务,PauseService 暂停服务,ResumeService 恢复服务,StopService 停止服务
}
}
// 验证是否能连接到远程计算机
public static bool RemoteConnectValidate(string host,string userName,string password)
{
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Username = userName;
connectionOptions.Password = password;
ManagementScope managementScope = new ManagementScope( "////" +host+ "//root//cimv2",connectionOptions) ;
try
{
managementScope.Connect();
}
catch
{
}
return managementScope.IsConnected;
}
// 获取所连接的计算机的所有服务数据
public string [,] GetServiceList()
{
string [,] services = new string [this.managementClass.GetInstances().Count,4];
int i = 0;
foreach(ManagementObject mo in this.managementClass.GetInstances())
{
services[i,0] = (string)mo["Name"];
services[i,1] = (string)mo["DisplayName"];
services[i,2] = (string)mo["State"];
services[i,3] = (string)mo["StartMode"];
i++;
}
return services;
}
public void Win32ServiceManager(string host,string userName,string password)
{
this.strPath = "////"+host+"//root//cimv2:Win32_Service";
this.managementClass = new ManagementClass(strPath);
if(userName!=null&&userName.Length>0)
{
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Username = userName;
connectionOptions.Password = password;
ManagementScope managementScope = new ManagementScope( "////" +host+ "//root//cimv2",connectionOptions) ;
this.managementClass.Scope = managementScope;
}
}
}
}
- .net下编程远程启动服务
- 命令行下启动系统服务 net start
- 远程启动服务
- Android启动远程服务
- 如何远程启动WebLogic服务
- win7启动telnet远程服务
- doc下运行net start MySQL启动不了服务
- 48.android服务service-启动远程服务
- ubuntu下允许root用户ssh远程登录及Linux下服务的启动
- Linux 下启动服务
- 远程启动服务和传送晚间
- scp远程下载和定时启动服务
- Android_08_创建,启动和关闭远程服务
- net logon服务无法启动
- 使用.net 2.0调用远程WCF服务
- [ZT]基于Spring实现远程服务编程
- linux下启动telnet服务
- 启动linux下ftp服务
- web下载的ActiveX控件自动更新
- Flex TextArea文本自动滚动方法
- 带参数线程
- 我也来谈谈三层架构
- 使用 RemObjects SDK 建立 WebService 应用
- .net下编程远程启动服务
- C#抽象类和接口总结
- oracle数据同步
- Hibernate 中的 Criteria 查询。
- 爱情:两个人的一辈子
- 浅淡sql如何随机取指定范围的数值--【叶子】
- 参照的汇编没有安装在系统上错误导致ocx注册失败的解决方法
- windows下无软驱,无光驱,无U盘安装Linux,双系统(Red Hat Enterprise Linux 5其他也可)
- 全面理解javascript的caller,callee,call,apply概念[转载]