代码:

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. Cloud Foundry技术全貌及核心组件分析

    原文链接:http://www.programmer.com.cn/14472/ 历经一年多的发展,Cloud Foundry的架构设计和实现有了众多改进和优化.为了便于大家了解和深入研究首个开源Pa ...

  2. 有关InitialContext()的困惑 <转>

    Context initial = new InitialContext();Object objref = initial.lookup("java:comp/env/ejb/Simple ...

  3. C# 关于调用office com组件导出Excel

    服务器环境: 环境为win2008 r2,系统为64位,程序是C#的winform.因为需要处理数据,然后生成Excel,耗时太长,就使用了多线程.winform程序是由计划任务启动,每天晚上去跑. ...

  4. 实用的IOS应用程序框架

    实用的IOS应用程序框架 目录 概述 概述

  5. Android 通过Socket 和服务器通讯

    Extends:(http://www.cnblogs.com/likwo/p/3641135.html) Android 通过Socket 和服务器通讯,是一种比较常用的通讯方式,时间比较紧,说下大 ...

  6. Mac - iPhone屏幕录制

    1. Mac电脑屏幕录制 1.1 文件->新建屏幕录制   1.2 点击红色按钮   1.3 截取需要录制的屏幕部分,点击开始录制   1.4 点击工具栏的停止按钮,停止录制   1.5 然后会 ...

  7. Activity工作流入门之HelloWorld

    Activity的在线安装地址为:http://www.activiti.org/designer/update/ 打开Eclipse -> Help -> Install New Sof ...

  8. HDCMS做异步加载!

    控制器的写法: //ajax 请求新闻列表 public function ajaxnewsList(){ $data = Q('sum'); $newsList = M('xinwen')-> ...

  9. 从零打造在线网盘系统之Struts2框架核心功能全解析

    欢迎浏览Java工程师SSH教程从零打造在线网盘系统系列教程,本系列教程将会使用SSH(Struts2+Spring+Hibernate)打造一个在线网盘系统,本系列教程是从零开始,所以会详细以及着重 ...

  10. 数据字典Data Dict

    数据字典 所有的数据表都属于数据库对象,每当创建一张数据表的时候,会自动在指定的数据字典表执行一个增加语句(这个增加语言我们是不知道的),数据字典的数据操作只能通过命令完成,不能直接使用SQL完成. ...