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的更多相关文章

  1. C#创建、安装、卸载、调试Windows Service(Windows 服务)的简单教程

    前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...

  2. 如何利用mono把.net windows service程序迁移到linux上

    How to migrate a .NET Windows Service application to Linux using mono? 写在最前:之所以用要把windows程序迁移到Linux上 ...

  3. 如何托管ASP.NET Core应用到Windows Service中

    (此文章同时发表在本人微信公众号"dotNET开发经验谈",欢迎右边二维码来关注.) 题记:正在构思一个中间件的设计,考虑是否既可以使用最新的技术,也可以兼顾传统的部署模式.所以有 ...

  4. 解决安装mysql的”A Windows service with the name MySQL already exists.“问题

    如果以前安装过mysql,卸载重装,很可能会碰到"A Windows service with the name MySQL already exists."这样的提示.即服务已经 ...

  5. 使用Windows Service Wrapper快速创建一个Windows Service

    前言 今天介绍一个小工具的使用.我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...

  6. Windows Service--Write a Better Windows Service

    原文地址: http://visualstudiomagazine.com/Articles/2005/10/01/Write-a-Better-Windows-Service.aspx?Page=1 ...

  7. 使用Python写Windows Service服务程序

    1.背景 如果你想用Python开发Windows程序,并让其开机启动等,就必须写成windows的服务程序Windows Service,用Python来做这个事情必须要借助第三方模块pywin32 ...

  8. Install Jenkins Slave as Windows Service

    https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+as+a+Windows+service SC 直接创建windows s ...

  9. 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 ...

  10. windows Service 创建部署

    Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序.Windows服务程序虽然是可执行的,但是它不像一般的可执行文件通过双击就 ...

随机推荐

  1. ubuntu下git输出的颜色变化

    (这些文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) 11点进家门,感觉很温暖哦. 以下是如何在用git的时候清晰的看出关键字的方法. $ vim ...

  2. python - PyQuery

    偶尔的机会,知道这么个扩展,手贱翻了下文档,发现似乎挺有意思,遂记录一二. what: 这是一个python版本的jquery,而且是后端执行的,至少官方是这么说的: pyquery allows y ...

  3. java 异常处理 Throwable Error 和Exception

    Java异常类层次结构图:       异常的英文单词是exception,字面翻译就是“意外.例外”的意思,也就是非正常情况.事实上,异常本质上是程序上的错误,包括程序逻辑错误和系统错误. 比如使用 ...

  4. js 实现图片预加载 (js操作 Image对象属性complete ,事件onload 异步加载图片)

    通过js操纵DOM很多情况下都是为了实现和当前页html元素的异步载入,我谈谈对Image对象的一些认识.看个例子:<input type="button" name=&qu ...

  5. 通过关闭UseDNS和GSSAPIAuthentication选项加速SSH登录

    引自:http://www.cnblogs.com/wjoyxt/p/3790537.html More:http://blogread.cn/it/article/4719 通常情况下我们在连接 O ...

  6. Windows下配置Tomcat服务器

    Tomcat服务器是Apache开源基金会的一个项目,tomcat不仅能作为静态文件的服务器,也可以作为JSP/Servlet的web容器,而且使用广泛,性能也不错,那么下面来配置一个基本的基于tom ...

  7. 用数据表创建树_delphi教程

    数据库结构:字段 类型ID 整型 索引(无重复)name 文本father 整型 //tree初始化procedure TForm1.FormActivate(Sender: TObject);var ...

  8. Java for LeetCode 198 House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  9. 化工厂装箱员(洛谷 P2530)

    题目描述 118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B:1%,C:0.01%,为了出售方便,必须 ...

  10. Tomcat AccessLog 格式化

    有的时候我们要使用日志分析工具对日志进行分析,需要对日志进行格式化,比如,要把accessLog格式化成这样的格式 c-ip s-ip x-InstancePort date time-taken x ...