安装服务:
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. iOS側拉栏抽屉效果Demo

    源代码下载 側拉栏抽屉效果Demo  须要导入第三方的类库例如以下: 抽屉效果所需第三方类库下载 效果:既能够两側都实现抽屉效果也可仅仅实现左側栏或者右側栏的抽屉效果           waterm ...

  2. MVC入门——列表页

    创建控制器UserInfoController using System; using System.Collections.Generic; using System.Linq; using Sys ...

  3. HBase GC日志

    HBase依靠ZooKeeper来感知集群成员及其存活性.假设一个server暂停了非常长时间,它将无法给ZooKeeper quorum发送心跳信息,其他server会觉得这台server已死亡.这 ...

  4. EasyRTMP实现将RTSP流转换成RTMP流实现RTSP直播转RTMP直播的功能

    本文转自EasyDarwin开源团队Kim的博客:http://blog.csdn.net/jinlong0603/article/details/52951311 EasyRTMP EasyRTMP ...

  5. mount available

    Mount (computing), the process of making a file system accessible mount (Unix), the utility in Unix- ...

  6. UVA 10529 - Dumb Bones(概率+区间dp)

    UVA 10529 - Dumb Bones option=com_onlinejudge&Itemid=8&category=518&page=show_problem&am ...

  7. appium(9)-uiautomator UiSelector

    uiautomator UiSelector Appium enables searching using UiSelectors. UiScrollable is also supported.// ...

  8. s:if

    <s:iterator value="value[3]" id="ques" status="s"> <s:if test ...

  9. jvm调试

    https://www.usenix.org/legacy/events/jvm01/full_papers/russell/russell_html/index.html

  10. android中样式和自定义button样式

    1)自定义button样式 一.采用图片方式 首先新建Android XML文件,类型选Drawable,根结点选selector,自定义一个文件名. 随后,开发环境自动在新建的文件里加了select ...