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. Python中的not, and, or

    logical_operator_lst = [ ('and 与运算',), ('or 或运算',), ('not 非运算',), ('逻辑运算符的优先级',), ('实例',), ('练习',), ...

  2. 不同级域名中的 Cookie 共享

    HTTP 响应头中 Set-Cookie 行未指定 domain 时则设置访问的域名 seliote.com 可以设置 seliote.com(也可以写成 .seliote.com 意思一样) 与 w ...

  3. PHP.27-TP框架商城应用实例-后台4-使用Gii生成品牌表的代码

    Gii安装[GII适用于商城项目] 将Gii文件夹复到application 是,访问http://xx.com/index.php/gii Gii规则[Gii使用规则与建表规则密切相关] 1.建表字 ...

  4. issubclasss/type/isinstance/callable/super

    issubclass() : 方法用于判断第一个参数是否是第二个参数的子子孙孙类. 语法:issubclass(sub, super) 检查sub类是否是 super 类的派生类 class A: p ...

  5. SpringMVC---四大注解

    SpringMVC四大注解 Component 通用标注,在不清楚使用哪个注解的时候,可以使用Component通用注解 Controller 标注web请求控制器 Service 标注Service ...

  6. Android log 引发的血案

    今天调试代码,我打印了一个东西: Log.d("WelcomeActivity", res.str); 结果总是代码执行不到这一行的下一行,程序也没有挂掉.后来,我自己去想各种可能 ...

  7. Retrofit get post query filed FiledMap

    直接请求型 1.如果是直接请求某一地址,写法如下: @GET("/record") Call getResult(); 2.如果是组合后直接请求,如/result/{id}写法如下 ...

  8. ElasticSearch学习笔记(三)-- 查询

    1. URISearch详解与演示 2. QueryDSL简介 3. 字段类查询简介及match-query 4. 相关性算分 5. match-phrase-query 6. query-strin ...

  9. SetConsoleCtrlHandler

    Excerpt: Registering a Control Handler Function   This is an example of the SetConsoleCtrlHandler fu ...

  10. Android学习记录(3)—Android中ContentProvider的基本原理学习总结

    一.ContentProvider简介        当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据.虽然使用其他方法也可以对外共享数据 ...