c# windows service
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text; namespace WindowsServiceTest
{
public partial class ServiceTest : ServiceBase
{
private DataSet _Ds = new DataSet();
//设置xml文件保存路径
private string _FilePath = @"D:\记录开关机时间.xml"; public ServiceTest()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
//每隔一分钟记录一次更新一次关机时间
System.Timers.Timer timer = new System.Timers.Timer(60000);
timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Click);
timer.AutoReset = true;
timer.Enabled = true; //判断文件是否存在
if (!System.IO.File.Exists(_FilePath))
{
CreateDataTable();
}
else
{
_Ds.ReadXml(_FilePath, XmlReadMode.ReadSchema);
} this.Add("开机时间", DateTime.Now);
this.Add("关机时间", DateTime.Now);
this.SaveToXml();
} protected override void OnStop()
{
this.Update("关机时间", DateTime.Now);
this.SaveToXml();
} private void Timer_Click(Object sender,System.Timers.ElapsedEventArgs e)
{
this.Update("关机时间",DateTime.Now);
this.SaveToXml();
} private void CreateDataTable()
{
System.Data.DataTable Dt = new DataTable("OPENCLOSE");
Dt.Columns.Add("TimeType", typeof(string));
Dt.Columns.Add("OperTime", typeof(DateTime));
_Ds.Tables.Add(Dt);
} //添加开关机时间记录
private bool Add(string TimeType, DateTime OperTime)
{
if (_Ds.Tables.Count == 0)
return false;
DataTable Dt = _Ds.Tables["OPENCLOSE"];
if (Dt == null)
return false;
DataRow dr = Dt.NewRow();
dr["TimeType"] = TimeType;
dr["OperTime"] = OperTime; Dt.Rows.Add(dr);
return true; } //更新关机时间
private bool Update(string OperTime, DateTime UpdateTime)
{
if (_Ds.Tables.Count == 0)
return false;
DataTable Dt = _Ds.Tables["OPENCLOSE"];
if (Dt == null)
return false;
DataRow Dr = Dt.Rows[Dt.Rows.Count - 1]; Dr["TimeType"] = OperTime;
Dr["OperTime"] = UpdateTime;
return true;
} //保存到xml文件
private void SaveToXml()
{
if (_Ds == null)
return;
_Ds.WriteXml(_FilePath, XmlWriteMode.WriteSchema);
}
}
}
服务安装脚本
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe WindowsServiceTest.exe
Net Start ServiceTest
sc config ServiceTest start= auto
pause
服务卸载脚本
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u WindowsServiceTest.exe
pause
c# windows service的更多相关文章
- C#创建、安装、卸载、调试Windows Service(Windows 服务)的简单教程
前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...
- 如何利用mono把.net windows service程序迁移到linux上
How to migrate a .NET Windows Service application to Linux using mono? 写在最前:之所以用要把windows程序迁移到Linux上 ...
- 如何托管ASP.NET Core应用到Windows Service中
(此文章同时发表在本人微信公众号"dotNET开发经验谈",欢迎右边二维码来关注.) 题记:正在构思一个中间件的设计,考虑是否既可以使用最新的技术,也可以兼顾传统的部署模式.所以有 ...
- 解决安装mysql的”A Windows service with the name MySQL already exists.“问题
如果以前安装过mysql,卸载重装,很可能会碰到"A Windows service with the name MySQL already exists."这样的提示.即服务已经 ...
- 使用Windows Service Wrapper快速创建一个Windows Service
前言 今天介绍一个小工具的使用.我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...
- Windows Service--Write a Better Windows Service
原文地址: http://visualstudiomagazine.com/Articles/2005/10/01/Write-a-Better-Windows-Service.aspx?Page=1 ...
- 使用Python写Windows Service服务程序
1.背景 如果你想用Python开发Windows程序,并让其开机启动等,就必须写成windows的服务程序Windows Service,用Python来做这个事情必须要借助第三方模块pywin32 ...
- Install Jenkins Slave as Windows Service
https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+as+a+Windows+service SC 直接创建windows s ...
- How to do code coverage test for windows service
First, instrument the exe or dll by command vsinstr -coverage the dll/exe second, start the performa ...
- windows Service 创建部署
Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序.Windows服务程序虽然是可执行的,但是它不像一般的可执行文件通过双击就 ...
随机推荐
- SSH 内网端口转发实战
导读 大家都知道SSH是一种安全的传输协议,用在连接服务器上比较多.不过其实除了这个功能,它的隧道转发功能更是吸引人. 如果两个内网之间的linux服务器需要互相登录,或需要互相访问内网某个端口,担忧 ...
- invert
http://docs.ruby-lang.org/en/2.0.0/Hash.html invert → new_hash Returns a new hash created by using h ...
- nginx学习(一):基本安装
转载自http://summervast.blog.51cto.com/690507/385511 注意:可能因版本不同,个别指令不起作用,需要注意版本灵活安装,我在安装时也遇到过此问题 开始学习ng ...
- 74 使用BitSet输出数组中的重复元素
[本文链接] http://www.cnblogs.com/hellogiser/p/using-bitset-to-print-duplicate-elements-of-array.html [题 ...
- 46. 对称子字符串的最大长度(ToDo)
[题目] 输入一个字符串,输出该字符串中对称的子字符串的最大长度.比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog”,因此输出4. [分析] 可能很多人都写过判断一个字符串 ...
- java 和 objective-c 动态获得类型信息
详细信息,请参看android官方文档中的class类的介绍 ,和苹果的官方文档Objective-C Runtime Reference java中常常听到反射reflection,在java的cl ...
- Java for LeetCode 164 Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- codeforces B. Making Sequences is Fun 解题报告
题目链接:http://codeforces.com/problemset/problem/373/B 题目意思:给出w,m和k,需要找出从m开始,可以有多少个连续的数(m+1,m+2,...)(在添 ...
- Eclipse中android工程C++文件中出现的莫名其妙的错误
大多数是std库相关的问题,例如 vector<int> v; v.push_back(23);//这句语法是没有错误的,但是每次执行Run As的时候就会报错 尝试1:在工程名右键-Cl ...
- 侃侃前端MVC设计模式
前言 前端的MVC,近几年一直很火,大家也都纷纷讨论着,于是乎,抽空总结一下这个知识点.看了些文章,结合实践略作总结并发表一下自己的看法. 最初接触MVC是后端Java的MVC架构,用一张图来表示之— ...