我的第一个Windows服务
代码:
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服务的更多相关文章
- 为MongoDB创建一个Windows服务
一:选型,根据机器的操作系统类型来选择合适的版本,使用下面的命令行查询机器的操作系统版本 wmic os get osarchitecture 二:下载并安装 附上下载链接 点击安装包,我这里是把文件 ...
- C#创建、安装一个Windows服务
关于WIndows服务的介绍,之前写过一篇: http://blog.csdn.net/yysyangyangyangshan/article/details/7295739.可能这里对如何写一个服务 ...
- tomcat创建一个windows服务
具体步骤如下: 1.把JDK解压到C:\Program Files\Java下,Tomcat解压到D:\tomcat下 2.配置环境变量 JAVA_HOME:C:\Program Files\Java ...
- 写了一个Windows服务,通过C#模拟网站用户登录并爬取BUG列表查询有没有新的BUG,并提醒我
写了一个Windows服务,通过C#模拟网站用户登录并爬取BUG列表查询有没有新的BUG,并提醒我 1.HttpUtil工具类,用于模拟用户登录以及爬取网页: using System; using ...
- 创建第一个windows服务
windows服务应用程序是一种长期运行在操作系统后台的程序,它对于服务器环境特别适合,它没有用户界面,不会产生任何可视输出,任何用户输出都回被写进windows事件日志. 计算机启动时,服务会自动开 ...
- [翻译] 使用 .NET Core 3.0 创建一个 Windows 服务
原文: .NET Core Workers as Windows Services 在 .NET Core 3.0 中,我们引入了一种名为 Worker Service 的新型应用程序模板.此模板旨在 ...
- 写一个Windows服务
做了两个和Windows服务有关的项目了,最开始的时候没做过,不懂,现在明白了许多.需要注意的是,如果不想登录什么的,最后在添加安装程序的那里选择那个字长的右键属性,把启动方式改为local syst ...
- C# Asp.net 制作一个windows服务
那下面就来说说如何制作一个服务来 实现开机自动启动,每隔一段时间向student表中插入数据. 步骤: 1) 新建项目 ---> Windows 服务 2) 拖放Times控件 工具箱中 ...
- 程序自动化需要一个Windows服务
前段时间,写了一个SPC to SQL数据传输的小功能,用户不太想用手执行或有可能忘记操作.解决这个问题,Insus.NET原本是使用windows的任务管理执行的,但觉得并不太理想,因此又得写一个W ...
随机推荐
- Java可视化JVM监控软件
jdk自带.jdk安装目录下 1.JConsole 选择 不安全 可用不多 2.VisualVM
- 【BZOJ4372】烁烁的游戏 动态树分治+线段树
[BZOJ4372]烁烁的游戏 Description 背景:烁烁很喜欢爬树,这吓坏了树上的皮皮鼠.题意:给定一颗n个节点的树,边权均为1,初始树上没有皮皮鼠.烁烁他每次会跳到一个节点u,把周围与他距 ...
- 【BZOJ4452】[Cerc2015]Export Estimate 并查集
[BZOJ4452][Cerc2015]Export Estimate Description 给你一个n个点m条边的无向图,每条边有权值,我们可以选择一个整数lim来生成一个新的图,过程如下: 1 ...
- angular -- ng-ui-route路由及其传递参数?script标签版
考虑到 多视图等因素,所以 angular 的路由考虑使用 ng-ui-route来做,而不使用 ng-route来做! <!DOCTYPE html> <html lang=&qu ...
- 树链剖分-点的分治(点数为k且距离最长的点对)
hdu4871 Shortest-path tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 130712/130712 K ( ...
- 170718、springboot编程之发送邮件
Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看如何在Spring Boot中使用Jav ...
- ubuntu16.04下安装opencv-3.1.0及其扩展模块opencv_contrib
步骤1.安装依赖项 sudo apt--dev pkg-config libavcodec-dev libavformat-dev libswscale-dev 可选的 sudo apt--dev l ...
- The Backpropagation Algorithm
https://page.mi.fu-berlin.de/rojas/neural/chapter/K7.pdf 7.1 Learning as gradient descent We saw in ...
- PHP通过curl模拟POST上传文件,5.5之前和之后的区别
首先先要着重提一下,只要是做和项目有关的开发,首先按把环境中各个服务的版本保持一致,否则出些莫名其妙的错我,让你百爪挠心却不知哪里的问题.这里就要说下curl_setopt($ch, CURLOPT_ ...
- 山寨HTML5API classList类
preface 认为自己去写一些类,你真的会找到自己不足的地方.事实上厉害不是你实现一个类.而是你怎样去设计一个类,能让开发人员更加easy操作. 对于这个操作样式,能够通过javascript訪问s ...