代码:

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. PHP内存缓存功能memcached

    PHP内存缓存功能memcached: http://blog.csdn.net/fangaoxin/article/details/6223383

  2. IIS7的HTTP到HTTPS的重定向

    方法一设置IIS 从HTTP到HTTPS的IIS7中的所有流量重定向,将确保用户始终安全地访问该网站.有许多不同的方法来设置一个IIS7从HTTP重定向到HTTPS和一些比别人更好.理想的HTTP到H ...

  3. K-均值聚类(K-means)算法

    https://www.cnblogs.com/ybjourney/p/4714870.html 最近在看<机器学习实战>这本书,因为自己本身很想深入的了解机器学习算法,加之想学pytho ...

  4. 系统中同时有 python2和 python3,怎么让 ipython 选择不同的版本启动?

    已经安装的情况下: > which ipython /usr/local/bin/ipython > cat /usr/local/bin/ipython  #!/usr/local/op ...

  5. 运行mlflow命令报错 The 'nose' distribution was not found and is required by nose-exclude

    安装好mlflow之后命令行运行: mlflow 得到报错: 解决: sudo pip3 install nose

  6. 构建vue项目(vue 2.x)时的一些配置问题(持续更新)

    基于前文,使用vue-cli脚手架工具构建项目,并使用了webpack,那么我在项目中遇到的一些与配置相关的问题将在这里进行汇总. 1.代码检查问题 由于我们在构建项目时,使用了Eslint对我们的项 ...

  7. Python开发【Django】:分页、Cookie和Session

    分页 1.简单分页 涉及xss攻击,需要用到mark_safe方法,使用此方法字符串传输到后端后,已html形式显示,而非字符串 HTML文件: <!DOCTYPE html> <h ...

  8. Ubuntu下安装Nginx详细步骤

    Nginx安装之前需要三个支持: 模块依赖性 ①gzip 模块需要 zlib 库 ②rewrite 模块需要 pcre 库 ③ssl 功能需要 openssl 库 预先编译好的包: sudo apt- ...

  9. receive.denyCurrentBranch 推送错误解决

    场景: 1.搭建Ok了一git服务器 2.本机上的现有源码,现在想纳入git源码管理 操作: 1.服务器上创建了工程仓库 git init 2. 客户端使用tortoisegit添加并提交要纳入源码管 ...

  10. ubuntu16.04 安装指定版本Node,升级npm到指定版本

    一.安装配置Node 1.下载(64位系统) wget https://nodejs.org/download/release/v10.1.0/node-v10.1.0-linux-x64.tar.g ...