社区
C#
帖子详情
如何实现开机自动启动一个Windows服务(服务为手动启动),类似于SqlServer服务控制器上的“当启动OS时自动启动服务”?
yxshu
2004-10-02 10:08:44
同题。
...全文
2224
5
打赏
收藏
如何实现开机自动启动一个Windows服务(服务为手动启动),类似于SqlServer服务控制器上的“当启动OS时自动启动服务”?
同题。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
5 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
qqqdong
2004-10-04
打赏
举报
回复
using System;
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;
}
}
}
yxshu
2004-10-03
打赏
举报
回复
我的意思是能不能通过类似于SQLServer服务管理器上的复选框实现:选中后,下次开机时自动运行服务,未选中则下次开机不运行。而不是手动的修改系统的系统的服务管理器。
wangxt
2004-10-03
打赏
举报
回复
在服务管理器中,选择某一服务,点右键选属性,把启动类型改为自动就行了
popcorn
2004-10-03
打赏
举报
回复
研究一下注册表:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
节中对应的服务的项的键值:
Start=2的时候就是设置为自动,3为手动,4为禁用.
在点CHECKBOX的时候更改一下注册表就可以了.
qqqdong
2004-10-02
打赏
举报
回复
在运行中输入:services.msc
在服务管理中将这个服务的启动参数设置为"自动"
Windows
Cluster集群和SQL Server数据库集群实战
本套课程讲解的知识点是
windows
管理员和数据库管理员(DBA)必须掌握的... 课程适用于所有版本的
Windows
(2012/2016/2019)和所有版本的SQL Server数据库(2008/2012R2/2014/2019) 相信这套课程学员看完会有很大的收获。
SQL SERVER
SQL Server数据库文件恢复技术SQL Server数据库备份有两种方式,一种是使用BACKUP DATABASE将数据库文件备份出去,另外一种就是直接拷贝数据库文件mdf和日志文件ldf的方式。下面将主要讨论一下后者的备份与恢复。...
Windows
7
开机
加速全攻略
-------《
Windows
7
开机
加速全攻略》全文导航--------
Windows
7
开机
加速全攻略一:序章
Windows
7
开机
加速全攻略二:系统
开机
原理篇
Windows
7
开机
加速全攻略三:
开机
设置优化
Windows
7
开机
...
Windows
7 个人
服务
详解及优化
Windows
7 个人
服务
详解及优化! 这是我个人根据网上提供的
服务
优化说明来修改的(【】号里就是我自己设定的),我是以安装了卡巴斯基和金山网镖的前提下关闭一些系统自带的防火墙和安全的
服务
,家庭电脑使用不错。...
Windows
2000
服务
器管理全攻略
即使是在
windows
网络安全设置方面,虽然网络上有着不少的关于这个方面的话题,但是这些文章当中并没有完全彻底的贯彻“最少的
服务
+最小的权限=最大的安全”这一原则,笔者由于前段
时
间工作于一家虚拟主机提供商,因...
C#
110,568
社区成员
642,567
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章