代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks; namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
//定时器
System.Timers.Timer t = null; public Service1()
{
InitializeComponent();
//启用暂停恢复
base.CanPauseAndContinue = true; //每5秒执行一次
t = new System.Timers.Timer();
//设置是执行一次(false)还是一直执行(true);
t.AutoReset = true;
//是否执行System.Timers.Timer.Elapsed事件;
t.Enabled = true;
//到达时间的时候执行事件(theout方法);
t.Elapsed += new System.Timers.ElapsedEventHandler(theout);
} protected override void OnStart(string[] args)
{
string state = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "启动";
WriteLog(state);
} protected override void OnStop()
{
string state = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "停止";
WriteLog(state);
}
//恢复服务执行
protected override void OnContinue()
{
string state = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "继续";
WriteLog(state);
t.Start();
} //暂停服务执行
protected override void OnPause()
{
string state = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "暂停";
WriteLog(state); t.Stop();
} public void WriteLog(string str)
{
using (StreamWriter sw = File.AppendText(@"d:\service.txt"))
{
sw.WriteLine(str);
sw.Flush();
}
} public void theout(object source, System.Timers.ElapsedEventArgs e)
{ WriteLog("theout:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
}
}
}

打开ProjectInstaller,修改serviceInstaller1组件属性

Description= 我的服务备注                       服务备注说明

DisplayName=我的服务                            服务友好名字

ServiceName=MyService                         安装服务器名字

StartType=Automatic                                服务类型

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

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

Disabled     服务无法启动。

并设计serviceProcessInstaller1的属性Account=LocalSystem

如果将Account设置成User,安装时就会:

安装windows服务要有installutil.exe,这个在安装vs的时候已经有了

看看自己windows服务的系统版本和net版本找到installutil.exe,

我的是在:C:\Windows\Microsoft.NET\Framework\v4.0.30319

并把它复制到新建服务的bin/debug里面,就是跟windows服务的exe在一个文件夹里,这样后面安装比较好

安装

dos到windows服务所在文件夹

installutil.exe WindowsService1.exe

安装好了以后可以在服务里面查看

卸载

installutil.exe -u WindowsService.exe

点击服务启动

报错

这个只要把服务权限选项卡设置everyone就可以了

查看输出文件

第三个启动是重新启动,服务先停后启

自己手动编写代码安装,卸载windows服务

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceProcess; namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string serviceName = "Service1";
private void button1_Click(object sender, EventArgs e)
{
string[] args = { "WindowsService1.exe" };//要安装的服务文件(就是用 InstallUtil.exe 工具安装时的参数)
ServiceController svcCtrl = new ServiceController(serviceName);
if (!ServiceIsExisted(serviceName))
{
try
{
System.Configuration.Install.ManagedInstallerClass.InstallHelper(args);
MessageBox.Show("服务安装成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
else
{
MessageBox.Show("该服务已经存在,不用重复安装。");
}
} private void button2_Click(object sender, EventArgs e)
{
try
{
if (ServiceIsExisted(serviceName))
{
//UnInstall Service
System.Configuration.Install.AssemblyInstaller myAssemblyInstaller = new System.Configuration.Install.AssemblyInstaller();
myAssemblyInstaller.UseNewContext = true;
myAssemblyInstaller.Path = "WindowsService1.exe";
myAssemblyInstaller.Uninstall(null);
myAssemblyInstaller.Dispose();
MessageBox.Show("卸载服务成功!");
}
else
{
MessageBox.Show("服务不存在!");
} }
catch (Exception)
{
MessageBox.Show("卸载服务失败!");
}
} /// <summary>
/// 检查指定的服务是否存在
/// </summary>
/// <param name="serviceName">要查找的服务名字</param>
/// <returns></returns>
private bool ServiceIsExisted(string svcName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName == svcName)
{
return true;
}
}
return false;
}
}
}

要把生成的exe跟要安装的windows服务exe放在一起,这样可以不用instuallutil.exe就可以安装,卸载了

在卸载是可能会出现服务没有被卸载,状态变成禁用的情况,这种请把服务管理窗口关掉就可以了

源码

=======================================

使用Windows批处理文件,例如添加一个Rongzi.RZR.ProcessExcelServie服务

install.bat:

sc create "Rongzi.RZR.ProcessExcelServie" binpath= "%~dp0Rongzi.RZR.ProcessExcel.Sub.exe"
sc description Rongzi.RZR.ProcessExcelServie "东方融资网融资人粒子导入数据服务"
sc failure "Rongzi.RZR.ProcessExcelServie" reset= actions= restart//restart//restart/
sc start "Rongzi.RZR.ProcessExcelServie"

uninstall.bat:

sc stop "Rongzi.RZR.ProcessExcelServie"
sc delete "Rongzi.RZR.ProcessExcelServie"

http://blog.csdn.net/angle860123/article/details/17375895

https://www.cnblogs.com/pingming/p/5108947.html

我的第一个Windows服务的更多相关文章

  1. 为MongoDB创建一个Windows服务

    一:选型,根据机器的操作系统类型来选择合适的版本,使用下面的命令行查询机器的操作系统版本 wmic os get osarchitecture 二:下载并安装 附上下载链接 点击安装包,我这里是把文件 ...

  2. C#创建、安装一个Windows服务

    关于WIndows服务的介绍,之前写过一篇: http://blog.csdn.net/yysyangyangyangshan/article/details/7295739.可能这里对如何写一个服务 ...

  3. tomcat创建一个windows服务

    具体步骤如下: 1.把JDK解压到C:\Program Files\Java下,Tomcat解压到D:\tomcat下 2.配置环境变量 JAVA_HOME:C:\Program Files\Java ...

  4. 写了一个Windows服务,通过C#模拟网站用户登录并爬取BUG列表查询有没有新的BUG,并提醒我

    写了一个Windows服务,通过C#模拟网站用户登录并爬取BUG列表查询有没有新的BUG,并提醒我 1.HttpUtil工具类,用于模拟用户登录以及爬取网页: using System; using ...

  5. 创建第一个windows服务

    windows服务应用程序是一种长期运行在操作系统后台的程序,它对于服务器环境特别适合,它没有用户界面,不会产生任何可视输出,任何用户输出都回被写进windows事件日志. 计算机启动时,服务会自动开 ...

  6. [翻译] 使用 .NET Core 3.0 创建一个 Windows 服务

    原文: .NET Core Workers as Windows Services 在 .NET Core 3.0 中,我们引入了一种名为 Worker Service 的新型应用程序模板.此模板旨在 ...

  7. 写一个Windows服务

    做了两个和Windows服务有关的项目了,最开始的时候没做过,不懂,现在明白了许多.需要注意的是,如果不想登录什么的,最后在添加安装程序的那里选择那个字长的右键属性,把启动方式改为local syst ...

  8. C# Asp.net 制作一个windows服务

    那下面就来说说如何制作一个服务来 实现开机自动启动,每隔一段时间向student表中插入数据. 步骤:  1)   新建项目 ---> Windows 服务 2) 拖放Times控件 工具箱中 ...

  9. 程序自动化需要一个Windows服务

    前段时间,写了一个SPC to SQL数据传输的小功能,用户不太想用手执行或有可能忘记操作.解决这个问题,Insus.NET原本是使用windows的任务管理执行的,但觉得并不太理想,因此又得写一个W ...

随机推荐

  1. jQuery回溯

    1.jQuery有个很好用的方法是 end(); 2.在进行链式操作时,使用end方法,可以回溯到上一个jQuery对象. 3.实现原理: jQuery内部有一个对象栈,当形成新的对象时,会将新对象推 ...

  2. sencha touch 入门系列 (六)sencha touch运行及代码解析(下)

    接着上一讲,通过index.html里development.js对app.json里js资源文件的解析,app.js便被index.html引入了, app.js是整个项目的程序入口,在项目完成时使 ...

  3. MySQL Server has gone away报错原因汇总分析(转自:http://cenalulu.github.io/mysql/mysql-has-gone-away/)

    原因1. MySQL 服务宕了 判断是否属于这个原因的方法很简单,执行以下命令,查看mysql的运行时长 $ mysql -uroot -p -e "show global status l ...

  4. mysql max_allowed_packet参数值改大后,莫名被还原

    mysql数据库用innodb引擎,mysql max_allowed_packet在my.cnf中值加大后,够一段时间,系统会莫名把这个参数的值改小. innodb_buffer_pool_size ...

  5. CentOS7使用yum安装LNMP环境以后无法打开php页面

    CentOS7使用yum安装LNMP环境以后无法打开php页面 页面提示为File not found 查看nginx错误日志/var/log/nginx/error.log提示如下 原因分析 ngi ...

  6. pta 习题集 数列求和-加强版

    给定某数字AA(1≤A≤91≤A≤9)以及非负整数NN(0≤N≤1000000≤N≤100000),求数列之和S=A+AA+AAA+⋯+AA⋯AS=A+AA+AAA+⋯+AA⋯A(NN个AA).例如A ...

  7. 关于java web的笔记2018-01-12

    需求:1.写一个商品类,有商品编号.商品名称.商品分类.商品单价属性.2.写一个商品条目信息类,有商品和数量两个属性,有商品总价格方法.3.写一个购物车类,有添加商品方法.查看订单信息,删除商品,修改 ...

  8. vue.js(四)

    由于组件内容太多又特别关键,我决定在官网教程的基础上,加上自己的理解,针对每个内容详细记录一下  1.注册组件 ①全局注册 //首先创建组件 Vue.component('blog-post', { ...

  9. 关于Controller层返回JSON字符串

    /** * 导入jackson包. * @param pn * @return */ @RequestMapping("/emps") @ResponseBody public M ...

  10. 【Linux】通过top语句可以查看压力测试的实时服务器状态。(可以通过百度Linux top查看相关内容)

    Linux实时查看服务器状态的两个语句 1.显示基本服务器监控状态语句如下:linux top 在这里输入 主要先看服务器负载高不高,高了后能否降下来,再看网络,io,数据库状态. 是有一个工具可以监 ...