前言 :

最近在处理应用程序安装的相关问题。
系统内有使用Window Time Service来完成时间同步的功能。

但在启动这个服务的时候。
却发现使用ManagementObject Class控制 WMI的这种方式,
无法将Windows Service启动类型设定为「自动(延迟开始)」。

使用Google搜寻之后,
找到了可以使用 Windows SC命令,来做Windows Service的管理。
并且这个方式,可以将Windows Service启动类型设定为「自动(延迟开始)」。

本篇文章简单纪录,
.NET应用程序如何使用Windows SC命令,来做Windows Service启动类型的设定。

方法 :

整个方法思路为
1. 执行一个隐形的 cmd.exe。
2. cmd.exe 呼叫 Windows SC命令来控制Windows Service启动类型。
3. 隐形的cmd.exe无法取得执行讯息,会无从判断执行错误。
  采用限制输入、资料验证、遇时处理等等方法,来尽量减少执行错误的机率。

下列的程序将上述动作封装为函式,
使用之前需要先加入System.ServiceProcess参考,直接呼叫即可设定Windows Service启动类型。

使用范例 :

SetWindowsServiceStartupType("w32time", StartupType.AutomaticDelayed);

原始码 :

public enum StartupType
{Automatic,Manual,Disabled,AutomaticDelayed,
}public static void SetWindowsServiceStartupType(String serviceName, StartupType startupType, int timeoutMilliseconds = 0)
{// ServiceNameif (string.IsNullOrEmpty(serviceName) == true){throw new ArgumentException("serviceName");}// StartTypestring startupTypeString = string.Empty;switch (startupType){case StartupType.Automatic: startupTypeString = "auto"; break;case StartupType.Manual: startupTypeString = "demand"; break;case StartupType.Disabled: startupTypeString = "disabled"; break;case StartupType.AutomaticDelayed: startupTypeString = "delayed-auto"; break;default: throw new ArgumentException("startupType");}// TimeoutMillisecondsif (timeoutMilliseconds < 0){throw new ArgumentException("timeoutMilliseconds");}// Check Existedbool isExisted = false;foreach (ServiceController serviceController in ServiceController.GetServices()){if (string.Compare(serviceController.ServiceName, serviceName, true) == 0){isExisted = true;break;}}if (isExisted == false) throw new Exception("Service not existed.");// Execute     String result = string.Empty;using (System.Diagnostics.Process process = new System.Diagnostics.Process()){process.StartInfo.FileName = "cmd.exe";process.StartInfo.Arguments = string.Format("/c sc config {0} start= {1}", serviceName, startupTypeString);process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;process.StartInfo.CreateNoWindow = false;process.Start();if (timeoutMilliseconds == 0){                    process.WaitForExit();                   }else{if (process.WaitForExit(timeoutMilliseconds) == false){process.Kill();throw new System.TimeoutException();}}}
}

转载于:https://www.cnblogs.com/clark159/archive/2012/01/04/2311930.html

[.NET] : 设定Windows Service启动类型相关推荐

  1. WIN10 windows installar 启动类型灰色不可更改

    WIN10 windows installar 启动类型灰色不可更改 今天想尝试禁止WIN10系统安装软件,在win7中只需要将 windows installar 启动类型改为"禁止&qu ...

  2. Windows Service:用C#创建Windows Service

    现在的.NET框架已经为Windows service的开发提供足够强大的支持,你只需要关注service所要实现的逻辑,而完全不用关心service底层是如何实现的,相比以前用MFC来说,真是质的飞 ...

  3. 启动和停止一个服务,修改服务的启动类型 Start and Stop Service for windows

    public void PreTest()         {             SetServiceLaunch(); // 修改服务的启动类型             ServiceCont ...

  4. Windows 无法启动 VMware Authorization Service 服务

    问题: 安装虚拟机后,虚拟机内的系统无法启动,弹出 "Windows 无法启动 VMware Authorization Service 服务(位于 本地计算机 上),错误 1068: 依赖 ...

  5. 无法连接到本地计算机上,Windows无法启动VMware USB Arbitration Service服务(位于本地计算机上)。解决方法...

    在windows电脑上,我们在用VMware 的时候,都会遇到各种各样的问题,特别是VMware USB Arbitration Service服务这个问题: "VMware USB Arb ...

  6. windows系统禁止更改服务启动类型_Win10系统关闭自动更新方法

    Windows 10是微软最新的操作系统,系统上的自动更新功能可以保障Win10系统处于最新的状态,不过对于普通用户来说并不是所有的更新都是必要的,且系统频繁更新会比较影响用户正常使用.下面就和大家分 ...

  7. 计算机启动类型bios,UEFI还是Legacy BIOS?如何确定Windows启动类型

    经常会有朋友在Q上询问关于系统方面的问题,多半都与安装方法有关,因此经常需要判断电脑上的Windows启动方式是什么,是UEFI还是BIOS呢?为了方便后来人查看,这里豆豆总结三个方法: 最装X方法: ...

  8. vs.net c# 安装、注册windows service服务,判断服务是否存在,是否启动

    vs.net c# 安装.注册windows service服务,判断服务是否存在,是否启动 一.安装服务: private void InstallService(IDictionary state ...

  9. 计算机网络服务无法启动,电脑遇到Windows无法启动world wide web publishing service服务,依赖服务或组无法启动怎么办-电脑自学网...

    电脑遇到Windows无法启动world wide web publishing service服务,依赖服务或组无法启动怎么办?最近有用户在使用IIS站点时,遇到不能启用world wide web ...

最新文章

  1. seq2seq nlp 这篇博文的引用相当好
  2. BootStrap之前奏响应式布局
  3. 电商系统的商品规格设计方案
  4. ue4 设置intellisence_UE4.22编辑器界面操控设置(4)
  5. JavaWeb中验证码的实现
  6. 事实--思维导图笔记
  7. 插件代码_我们开源了一款 SonarQube iOS 代码扫描插件
  8. CentOS7.6安装WirёGuαrd(二)
  9. 2008.5调试安装hp dl385 两台hp dl585
  10. php万能播放器代码,网页视频播放器程序代码(通用代码)
  11. centos lnmp一键安装
  12. 21年11月第二周 力扣每日一题记录
  13. meta的http-equiv属性
  14. Linux基础知识命令总结1
  15. 太牛了!我的阿里春招之路分享,值得收藏!
  16. ios8 系统定位问题
  17. ipp协议 服务器,IPP远程服务
  18. 单片机嵌入式实时操作系统RTOS---eOS学习入门级RTOS(原创)
  19. 如何巧妙回答离职原因
  20. 人体呼吸心跳感应雷达模块,智能家居传感技术,存在感应雷达应用

热门文章

  1. 各大厂商CTR广告预估模型的优缺点对比
  2. 京东下拉词框天猫下拉词框优化推广方法分享
  3. 输出高电平程序c语言,51用c语言怎么编程检测US-100超声波测距模块echo/rx引脚输出高电平时间长度,,谁能给我个模板...
  4. 在Android平台上发现新的恶意程序伪装成杀毒软件挟持设备
  5. 股票软件开发中全推与点播的区别
  6. python画笔的尺寸_Matplotlib 常用画图命令总结:使用 Python 在论文中画出一手漂亮的数据图...
  7. Chrome Inspect调试stetho出现空白的解决方法
  8. 超简单的memcached集群搭建
  9. HTML5的视频格式之争
  10. 百分点集团刘译璟:人工智能四大挑战及对策