安装服务:
installutil.exe E:\XTestDemo\X_15_WindowsService\bin\Debug\X_15_WindowsService.exe

卸载服务:
installutil.exe /u E:\XTestDemo\X_15_WindowsService\bin\Debug\X_15_WindowsService.exe

Windows服务:Microsoft Windows 服务(即,以前的 NT服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序。这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面。这使服务非常适合在服务器上使用,或任何时候,为了不影响在同一台计算机上工作的其他用户,需要长时间运行功能时使用。还可以在不同于登录用户的特定用户帐户或默认计算机帐户的安全上下文中运行服务。(摘自百度百科)

一:创建

二:属性

VS自动创建了一个Service1.cs的文件:

点击F4,查看各个属性的含义:

Autolog                 是否自动写入系统的日志文件

CanHandlePowerEvent     服务时候接受电源事件

CanPauseAndContinue          服务是否接受暂停或继续运行的请求

CanShutdown 服务是否在运行它的计算机关闭时收到通知,以便能够调用 OnShutDown 过程

CanStop                              服务是否接受停止运行的请求

ServiceName                       服务名称

三:功能

点击F7查看Service1.cs的源代码:

默认实现了OnStart和OnStop两个方法。

以向一个文本文件中写入数据操作为例:

   public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
} /// <summary>
/// 服务启动
/// http://www.cnblogs.com/babycool
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
string start = string.Format("{0}-{1}",DateTime.Now.ToString("yyyyMMddHHmmss"),"程序启动了。");
Log(start);
} /// <summary>
/// 服务停止
/// http://www.cnblogs.com/babycool
/// </summary>
protected override void OnStop()
{
string start = string.Format("{0}-{1}", DateTime.Now.ToString("yyyyMMddHHmmss"), "程序停止了。");
Log(start);
} /// <summary>
/// 系统关闭
/// http://www.cnblogs.com/babycool
/// </summary>
protected override void OnShutdown()
{
string start = string.Format("{0}-{1}", DateTime.Now.ToString("yyyyMMddHHmmss"), "电脑关闭了。");
Log(start);
} /*
/// <summary>
/// 服务暂停
/// http://www.cnblogs.com/babycool
/// </summary>
protected override void OnPause()
{ }
*/ /*
/// <summary>
/// 服务继续
/// </summary>
protected override void OnContinue()
{
base.OnContinue();
}
*/
/*
/// <summary>
/// 系统电源状态改变
/// </summary>
/// <param name="powerStatus"></param>
/// <returns></returns>
protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
{
return base.OnPowerEvent(powerStatus);
}
*/ void Log(string str)
{
string path = "E://def/6.txt";
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(str);
}
} }

四:安装程序

切换到 Service1.cs[设计] 界面,右击选择“添加安装程序”。

这时项目中就添加了一个新类 ProjectInstaller 和两个安装组件 ServiceProcessInstaller 和 ServiceInstaller。

选中“serviceInstaller1” 控件,F4打开属性面板,

Description       服务程序的描述信息

DisplayName     服务程序显示的名称

StartType        指定如何启动服务

Manual      服务安装后,必须手动启动

Automatic    每次计算机重新启动时,服务都会自动启动

Disabled     服务无法启动

选中“serviceProcessInstaller1” 控件,F4打开属性面板:

将serviceProcessInstaller类的Account属性改为 LocalSystem。

这样,不论是以哪个用户登录的系统,服务总会启动。

五:生成

右击 项目 选择生成 ,不能通过F5来直接运行服务项目。

六:安装卸载服务

选择 VS组件 “Visual Studio命令提示(2010)” 工具,并以“管理员身份运行"(win7、win8系统下)。

注意:这里必须选择“以管理员身份运行”,否则会报错。

  

从命令行运行 Installutil.exe 目录  命令,以项目中的已编译可执行文件所在的目录作为参数,安装服务:

1. 方法 1

因为Installutil.exe程序在 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ 目录下,需要通过cmd命令 "cd" 切换目录。

从命令行运行 Installutil.exe /u 目录   命令来卸载服务:

安装服务:
installutil.exe E:\XTestDemo\X_15_WindowsService\bin\Debug\X_15_WindowsService.exe

卸载服务:
installutil.exe /u E:\XTestDemo\X_15_WindowsService\bin\Debug\X_15_WindowsService.exe

1. 方法 2

找到 Installutil.exe 文件,并把它复制到 E:\XTestDemo\X_15_WindowsService\bin\Debug\ 目录

现在 Installutil.exe 程序在 E:\XTestDemo\X_15_WindowsService\bin\Debug\ 目录下,需要通过cmd命令 "cd" 切换目录。

安装服务:
installutil.exe X_15_WindowsService.exe

卸载服务:
installutil.exe X_15_WindowsService.exe

七:查看服务状态

在“计算机管理”中,服务 下可以看到刚刚安装的Service服务(cmd命令:services.msc---本地服务设置):

默认是停止状态。右击,选择“启动”,即可开启服务。

通过“属性”,可以查看到更详细的信息。

原文地址:http://www.cnblogs.com/babycool/p/3534786.html

相关参考:

用C#创建Windows服务(Windows Services) - Gsun - 博客园

C#开发Windows Services服务--服务安装失败的解决办法 - 无名小虾 - 博客园

C#应用服务器!包括(消息服务器,更新服务器,应用服务器)

WindowsService服务程序开发 安装和卸载的更多相关文章

  1. [开发笔记]-WindowsService服务程序开发

    Windows服务:Microsoft Windows 服务(即,以前的 NT服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可 ...

  2. WindowsService服务程序开发

    Windows服务:Microsoft Windows 服务(即,以前的 NT服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可 ...

  3. C#/.NET基于Topshelf创建Windows服务程序及服务的安装和卸载(极速,简洁)

    本文首发于:码友网--一个专注.NET/.NET Core开发的编程爱好者社区. 文章目录 C#/.NET基于Topshelf创建Windows服务的系列文章目录: C#/.NET基于Topshelf ...

  4. C#Windows Service服务程序的安装/卸载、启动/停止 桌面客户端管理程序设计

    C#Windows Service服务程序的安装/卸载.启动/停止 桌面客户端管理程序设计 关于Windows Service程序的安装与卸载如果每次使用命令行操作,那简直要奔溃了,太麻烦而且还容易出 ...

  5. [开发笔记]-使用bat命令来快速安装和卸载Service服务

    一般我们在编写完Service服务程序后,都是通过cmd命令提示窗口来安装或卸载服务,但频繁的在cmd窗口中去“拼”文件的路径着实让人“不能忍”.所以,我们需要一钟“更快捷”的方式来进行安装或者卸载操 ...

  6. Kurento应用开发指南(以Kurento 5.0为模板) 之中的一个:简单介绍,安装与卸载

    文件夹 1. Kurento是什么               3 2. Kurento简单介绍                       3 2.1 WebRTC媒体server         ...

  7. Windows Service 开发,安装与调试

    Visual Studio.net 2010 Windows Service 开发,安装与调试 本示例完成一个每隔一分钟向C:\log.txt文件写入一条记录为例,讲述一个Windows Servic ...

  8. .net Windows服务程序和安装程序制作图解 及 VS 2010创建、安装、调试 windows服务(windows service)

    .net Windows服务程序和安装程序制作 最近项目中用到window服务程序,以前没接触过,比较陌生,花了两天的时间学习了下,写了个简单的服务,但在制作安装程序的时候,参照网上很多资料,却都制作 ...

  9. .net Windows服务程序和安装程序制作图解

    最近项目中用到window服务程序,以前没接触过,比较陌生,花了两天的时间学习了下,写了个简单的服务,但在制作安装程序的时候,参照网上很多资料,却都制作不成功,可能是开发环境或项目配置的不同,这里把自 ...

随机推荐

  1. 基于注解的Sping AOP详解

    一.创建基础业务 package com.kang.sping.aop.service; import org.springframework.stereotype.Service; //使用注解@S ...

  2. The threads in the thread pool will process the requests on the connections concurrently.

    https://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html Most of the executor implem ...

  3. TControl,TWinControl和TGraphicControl的显示函数

    -------------------------- 显示隐藏刷新 -------------------------- TControl = class(TComponent)procedure S ...

  4. Linux就该这么学--命令集合4(文件目录管理命令)

    1.touch命令用于创建空白文件与修改文件时间:(touch [选项] [文件]) 对于在Linux中的文件有三种时间: 更改时间(mtime):内容修改时间(不包括权限的) 更改权限(ctime) ...

  5. linux定期判断网站可否打开

      wget http://www.shopindream.de/200.php --timeout=2 c_monitor=$? rm -rf 200.php if [ ! $c_monitor = ...

  6. xcode7和ios9下UIWebView不能加载网页的解决方法

    错误描述: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecu ...

  7. performSelector: 与 dispatch_time 异同

    iOS中timer相关的延时调用,常见的有NSObject中的performSelector:withObject:afterDelay:这个方法在调用的时候会设置当前runloop中timer,还有 ...

  8. iOS 判定string是不是中文字符

    +(BOOL)IsChinese:(NSString *)str { ; i< [str length];i++) { int a = [str characterAtIndex:i]; if( ...

  9. jmeter使用笔记——流程及常用组件配置

    添加线程组 线程数 :对应用户数, Ramp-Up: 多少秒启动这些线程,1秒代表1秒内启动设置的线程数,10秒代表10秒内启动线程数 循环次数: 每个线程执行线程组内的请求循环次数 调度器:可以对线 ...

  10. codeforces 463B Caisa and Pylons 解题报告

    题目链接:http://codeforces.com/problemset/problem/463/B 题目意思:Caisa 站在 0 pylon 并且只有 0 energy,他需要依次跳过1 pyl ...