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. 腾讯首页分辨手机端与pc端代码

    腾讯首页分辨手机端与pc端代码 自己在做网页的时候在腾讯网首页借鉴的代码. 代码: <!-- 移动适配JS脚本 --> <script type="text/javascr ...

  2. Hacker Cups and Balls Gym - 101234A 二分+线段树

    题目:题目链接 题意:有编号从1到n的n个球和n个杯子. 每一个杯子里有一个球, 进行m次排序操作,每次操作给出l,r. 如果l<r,将[l,r]范围内的球按升序排序, 否则降序排, 问中间位置 ...

  3. 笔记-pyton内置数据类型

    笔记-pyton内置数据类型 1.      简介 The principal built-in types are numerics, sequences, mappings, classes, i ...

  4. Android 内嵌 HTML5 并进行交互

    Android与HTML5的交互主要是两个部分, 与HTML5的交互以及与JavaScript的交互, 与HTML5的交互可以通过注册onclick事件转化为与JavaScript的交互 Androi ...

  5. Selenium+Python自动化之如何绕过登录验证码

    一.使用Fiddler抓包 1.一般登陆网站成功后,会生成一个已登录状态的cookie,那么只需要直接把这个值拿到,用selenium进行addCookie操作即可. 2.可以先手动登录一次,然后抓取 ...

  6. selenium fluentwait java实例

    本文转自:http://www.programcreek.com/java-api-examples/index.php?api=org.openqa.selenium.support.ui.Flue ...

  7. Kotlin中功能操作与集合(KAD 11)

    作者:Antonio Leiva 时间:Feb 2, 2017 原文链接:https://antonioleiva.com/functional-operations-collections-kotl ...

  8. 孤荷凌寒自学python第二十八天python的datetime.date模块

     孤荷凌寒自学python第二十八天python的datetime.date模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.toordinal() 此方法将访问从公元1年1月1日至当 ...

  9. Kickstart配置文件解析

    参考:https://www.douban.com/note/270359374/?type=likehttp://blog.51cto.com/molinux/548247http://debugo ...

  10. 201621123033 《Java程序设计》第9周学习总结

    第九次作业 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 //stream(),filter(),collect() ...