1、新建项目DemoService,并添加windows服务,命名DemoService

2、添加代码

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Configuration;
using System.Windows.Forms;
using System.Threading; namespace DemoService
{
public partial class DemoService : ServiceBase
{
private System.Timers.Timer timer1;
public DemoService()
{
InitializeComponent();
this.timer1 = new System.Timers.Timer();
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
} protected override void OnStart(string[] args)
{
System.Threading.Thread.Sleep();
LogUtil.WriteLog(" 服务开始启动");
this.timer1.Interval = * Convert.ToInt32(ConfigurationManager.AppSettings["TimeSpan"]);
this.timer1.Enabled = true;
//this.timer1.Start();
LogUtil.WriteLog(" 服务启动完成");
} protected override void OnStop()
{
LogUtil.WriteLog(" 服务正在停止");
this.timer1.Stop();
this.timer1.Enabled = false;
LogUtil.WriteLog(" 服务已经停止");
} private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
LogUtil.WriteLog(" 服务测试运行中...");
Thread.Sleep();
MonitorService.ShouldRestart = true;
}
}
}

3、添加安装类,并配置如下:

4、配置App.config:

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!--重启超时时间 单位:秒-->
<add key="RestartTimeOut" value="3600"/>
<!--执行时间间隔 单位:秒-->
<add key="TimeSpan" value="5"/>
<add key="LogPath" value="c:\\ProjectService\\LOG\\"/>
</appSettings>
</configuration>

5、LogUtil.cs

public class LogUtil
{
public static void WriteLog(string error)
{
string fileName = DateTime.Now.ToString("yyyy-MM-dd") + "DomainServiceLog.txt";
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(ConfigurationManager.AppSettings["LogPath"] + fileName, true))
{
sw.WriteLine("---------------------------------------------------------");
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + error);
}
} }

6、生成项目,执行如下脚本安装服务:

@echo off
set path=C:\Windows\Microsoft.NET\Framework\v4.0.30319\ @echo 服务安装中... InstallUtil.exe C:\ProjectService\DemoService.exe @echo 服务安装完成. pause

执行如下脚本启动服务:

@echo off

if not exist C:\\ProjectService\\LOG\\ (
md C:\\ProjectService\\LOG\\
) @echo 服务启动中... net start DemoService @echo 服务正在运行... pause

执行如下脚本停止服务:

@echo off

@echo 服务停止中...

net stop DemoService

@echo 服务已经停止.

pause

执行如下脚本卸载服务:

@echo off
set path=C:\Windows\Microsoft.NET\Framework\v4.0.30319\ @echo 服务卸载中... InstallUtil.exe /u C:\ProjectService\DemoService.exe @echo 服务卸载完成. pause

7、如果把命令写在一起,可以根据选择进行操作,命令如下:

@echo off
@echo ********************************************** rem 关闭自动输出 :begin echo 请输入 1:安装服务 2:启动服务 3:停止服务 4:卸载服务 其他:退出
rem 接收输入
set input=
set /p input= rem 输出得到的输入信息 rem echo 您输入的字符串是:%input%
if %input%==1 (
set path=C:\Windows\Microsoft.NET\Framework\v4.0.30319\
@echo 服务安装中...
InstallUtil.exe C:\ProjectService\DemoService.exe
@echo 服务安装完成.
goto begin
)
if %input%==2 (
set path=C:\Windows\System32\
if not exist C:\\ProjectService\\LOG\\ (
md C:\\ProjectService\\LOG\\
)
@echo 服务启动中...
net start DemoService
net start MonitorService
@echo 服务正在运行...
goto begin
)
if %input%==3 (
set path=C:\Windows\System32\
@echo 服务停止中...
net stop DemoService
net stop MonitorService
@echo 服务已经停止.
goto begin
)
if %input%==4 (
set path=C:\Windows\Microsoft.NET\Framework\v4.0.30319\
@echo 服务卸载中...
InstallUtil.exe /u C:\ProjectService\DemoService.exe
@echo 服务卸载完成.
goto begin
) goto end rem pause>null rem echo. rem 从begin标签出,再次运行 rem goto begin :end @echo **********************************************

完整代码:DemoService.rar

自定义Windows服务并实施安装的更多相关文章

  1. Windows服务创建及安装

    Windows服务创建及安装http://www.cnblogs.com/tuyile006/archive/2006/11/27/573654.html

  2. 关于windows服务的编写/安装/与调试

    前注: 首先,这篇文章是从网上转过来的,因为最近有个项目,需要编写一个Windows Service来定时执行程序,网上很容易找到了这篇文章,大概看了一下,文章讲的还是很详细的.不过这篇文章应该是.n ...

  3. .net windows 服务创建、安装、卸载和调试

    原文:http://blog.csdn.net/angle860123/article/details/17375895 windows服务应用程序是一种长期运行在操作系统后台的程序,它对于服务器环境 ...

  4. C# windows 服务编写及安装

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

  5. 基于C#&.net2.0的windows服务创建与安装

    起因:一台服务器中部署的程序,停电后未按照计划任务正常启动. 一.创建并配置Windows服务程序 开发工具VS2015 Framework版本2.0 1.新建Windows服务 2.在Service ...

  6. c# windows服务 一个项目安装多个服务

    创建windows服务就不讲解了,其它大神写太多了.这里只写一个项目安装多个服务的教程.如:http://www.cnblogs.com/zzgblog/p/4595839.html 首先按下图创建多 ...

  7. windows服务搭建(VS2019创建Windows服务不显示安装组件)

    1.创建windows服务应用 2.右键查看代码 3.写个计时器Timer  using System.Timers; 如上图,按tab键快速操作  会自动创建一个委托 改为下边的方式,打印日志来记录 ...

  8. 安装、部署... Windows服务 .net程序 安装 命令

    @echo offInstallutil.exe 程序目录 F:\test\TestWindows.exe 服务程序目录@sc start "服务名称"@sc config &qu ...

  9. 用VS制作的windows服务安装包 安装完后如何让服务自动启动

    vs 服务做成安装包,如何安装以后启动服务,只要在类名为projectinstaller的类中重写commit事件即可         public override void Commit(IDic ...

随机推荐

  1. git的使用入门

    写作目的: 快速的上手git版本控制+github神器进行基本的版本同步操作. 怎么做? 对于任意一个代码项目,使用git_bash进入到代码目录 如果没有进行过初始化操作:应当使用git init  ...

  2. perl语言入门总结-第3章-列表与数组

    1-列表list指的是标题的有序集合, 而数组(array)则是存储列表的变量. 更精确地说,列表指的是数据,而数组指的是变量. 访问数组中的元素 ] = "yabba"; ] = ...

  3. 笔记-python-functool-@wraps

    笔记-python-functool-@wraps 1.      wraps 经常看到@wraps装饰器,查阅文档学习一下 在了解它之前,先了解一下partial和updata_wrapper这两个 ...

  4. IDA 对 so 的动态调试

    将IDAPro根目录下dbgsrv 目录下的android_server(模拟器用android_x86_server,这里还是用真机好点)文件push 到安卓设备(比如/data/local/tmp ...

  5. 4 Vue.js 核心理念:数据驱动界面

    1 style样式放在body里面 <style type="text/css"> .ui.segment.container { width:700px; } p { ...

  6. Robolectric

    今天学习了单元测试框架,Robolectric.初步感觉,可能我测试的少,没有感觉Robolectric能有多大的帮助.虽然可以帮助创建activity.可以模拟点击事件.可是有什么呢. 好吧,讲下使 ...

  7. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 's.areaname' in 'field list'错误

    在使用mybatis框架做查询的时候,出现了如下错误: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown colum ...

  8. LDAP操作的两种方案

    最近由于项目需要研究了一下LDAP相关知识,感觉对没接触过的人来说还是有点坑的,所以记录下来给大家分享. 由于是第一次接触,就在网上搜了一些相关的文章,照着示例代码测试,却怎么也连不上LDAP服务器, ...

  9. 《Cracking the Coding Interview》——第10章:可扩展性和存储空间限制——题目7

    2014-04-24 22:06 题目:搜索引擎问题,如果有一列100台服务器的集群,用来响应查询请求,你要如何设计query分发和cache策略? 解法:query分发可以用计算数字签名并对机器数取 ...

  10. 【Linear Models for Binary Classification】林轩田机器学习基石

    首先回顾了几个Linear Model的共性:都是算出来一个score,然后做某种变化处理. 既然Linear Model有各种好处(训练时间,公式简单),那如何把Linear Regression给 ...