在运行中输入:services.msc在服务管理中将这个服务的启动参数设置为"自动"
解决方案 »
- 用C#写了个.dll给VB6调用,制作安装盘时必须把整个framework4.0打包进来吗?
- 为什么struct类型的以参数传给system.object类型?
- 求一个处理图片大小的类或例子
- 关于登录时弹窗与弹层之我见,拍砖的进来。
- 基于Windows操作系统小型局域网络入网自动化如何实现?
- dev中GridControl新问题!!!!(高分,在线等待)
- 条件编译的作用域
- C#分割图片问题?急
- ADO.net如何连接sybase数据库?
- 200分求解!!! c# ftp 上传断开后第二次上传的问题
- 在VC#.net中怎么将已有的一个Excel Sheet保存为一新的Excel文件?
- 如何实现基于Internet的报表管理软件
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
节中对应的服务的项的键值:
Start=2的时候就是设置为自动,3为手动,4为禁用.在点CHECKBOX的时候更改一下注册表就可以了.
using System.Management;namespace DkkNet.ControlRemoteServices
{
public class ControlRemoteServices
{
private string strPath;
private ManagementClass managementClass; public ControlRemoteServices():this(".",null,null) { }
public ControlRemoteServices(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;
}
} // 验证是否能连接到远程计算机
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 object GetServiceValue(string serviceName,string propertyName)
{
ManagementObject mo = this.managementClass.CreateInstance();
mo.Path = new ManagementPath(this.strPath+".Name=\""+serviceName+"\"");
return mo[propertyName];
} // 获取所连接的计算机的所有服务数据
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 string [,] GetServiceList(string serverName)
{
return GetServiceList(new string []{serverName});
} // 获取所连接的计算机的的指定服务数据
public string [,] GetServiceList(string [] serverNames)
{
string [,] services = new string [serverNames.Length,4];
ManagementObject mo = this.managementClass.CreateInstance();
for(int i = 0;i<serverNames.Length;i++)
{
mo.Path = new ManagementPath(this.strPath+".Name=\""+serverNames[i]+"\"");
services[i,0] = (string)mo["Name"];
services[i,1] = (string)mo["DisplayName"];
services[i,2] = (string)mo["State"];
services[i,3] = (string)mo["StartMode"];
}
return services;
} // 停止指定的服务
public string StartService(string serviceName)
{
string strRst = null;
ManagementObject mo = this.managementClass.CreateInstance();
mo.Path = new ManagementPath(this.strPath+".Name=\""+serviceName+"\"");
try
{
if((string)mo["State"]=="Stopped")//!(bool)mo["AcceptStop"]
mo.InvokeMethod("StartService",null);
}
catch(ManagementException e)
{
strRst =e.Message;
}
return strRst;
} // 暂停指定的服务
public string PauseService(string serviceName)
{
string strRst = null;
ManagementObject mo = this.managementClass.CreateInstance();
mo.Path = new ManagementPath(this.strPath+".Name=\""+serviceName+"\"");
try
{
//判断是否可以暂停
if((bool)mo["acceptPause"]&&(string)mo["State"]=="Running")
mo.InvokeMethod("PauseService",null);
}
catch(ManagementException e)
{
strRst =e.Message;
}
return strRst;
} // 恢复指定的服务
public string ResumeService(string serviceName)
{
string strRst = null;
ManagementObject mo = this.managementClass.CreateInstance();
mo.Path = new ManagementPath(this.strPath+".Name=\""+serviceName+"\"");
try
{
//判断是否可以恢复
if((bool)mo["acceptPause"]&&(string)mo["State"]=="Paused")
mo.InvokeMethod("ResumeService",null);
}
catch(ManagementException e)
{
strRst =e.Message;
}
return strRst;
} // 停止指定的服务
public string StopService(string serviceName)
{
string strRst = null;
ManagementObject mo = this.managementClass.CreateInstance();
mo.Path = new ManagementPath(this.strPath+".Name=\""+serviceName+"\"");
try
{
//判断是否可以停止
if((bool)mo["AcceptStop"])//(string)mo["State"]=="Running"
mo.InvokeMethod("StopService",null);
}
catch(ManagementException e)
{
strRst =e.Message;
}
return strRst;
}
}
}