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. @version ||= version

    # -*- encoding : utf-8 -*- class InterfaceBaseController < ActionController::Base private def set ...

  2. java笔试三

    请问如何不使用第三个变量交换两个变量值?     例如   int   a=5,b=10:     如何不使用第三个变量交换a,b的值? public class T { public static ...

  3. 暑假热身 A. GCC

    GCC编译器是一个由GNU项目维护的编译系统,它支持多种编程语言的编译.但是它并不包含数学运算符“!”.在数学中,这个符号代表阶乘.表达式n!的意思是从1到n的所有整数的乘积. 例如,4!=4*3*2 ...

  4. Linux Tomcat 简介

    如今,基于Web的应用越来越多,传统的Html已经满足不了如今的需求.我们需要一个交互式的Web,于是便诞生了各种Web语言.如Asp,Jsp,Php等.当然,这些语言与传统的语言有着密切的联系,如P ...

  5. total commander相关设置

    一. 中文语言包 在官方网站上提供有简体中文语言包,下面的说明以此为准.下载的语言包放至Total Commander安装目录下的Language子目录中.从菜单“Configuration”→“Op ...

  6. scp失效问题

    1.症状 (1)登陆到服务器A(已在本机用ssh-add, ssh -A),scp 到服务器B时提示public key有问题: (2)/tmp/下没有ssh-XXX目录 2.原因 服务器A被pupp ...

  7. linux下搭建svn版本控制软件

    svn作为曾经流行的版本控制控制软件,它优异的版本控制功能在有意无意间已经深入IT人的工作了.然而虽然有后起之秀的git,但现在使用svn的项目并不在少数.最近有个项目需要协同开发,由于对svn用得比 ...

  8. HTML页面表单输入框去掉鼠标选中后边框变色的效果

    标题说的有些含糊,实在不知道怎么描述了,就简单说一下吧,一个最简单的表单,代码如下,没有任何样式和名字, <!DOCTYPE html> <html> <head> ...

  9. emmet中文文档 (转载)

    Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...

  10. eclipse 切换svn账号

    1. 查看你的Eclipse中使用的是什么SVN Interface,位置在          windows > preference > Team > SVN 2. 如果是用的J ...