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. iOS团队开发者测试

    那么你需要在你下载证书的那个电脑上从钥匙串-->选择证书-->右键到处证书,保存为.p12的证书,以后这个证书拷贝到任何电脑上去都是可以使用的! 本来只有一台电脑可以测试, 现在要团队开发 ...

  2. ubuntu14.04安装OpenVirteX

    官网链接: http://ovx.onlab.us/getting-started/installation/ step1: System requirements: Recommended 4 Co ...

  3. div设置边框黑框显示

    style="width:756px; height:68px; border:1px solid #000000;"

  4. Java for LeetCode 141 Linked List Cycle

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  5. Java中删除文件、删除目录及目录下所有文件

    转载自:http://www.cnblogs.com/eczhou/archive/2012/01/16/2323431.html 功能:删除某个目录及目录下的所有子目录和文件 知识点:File.de ...

  6. CodeForces - 416A (判断大于小于等于 模拟题)

    Guess a number! Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Sub ...

  7. Powershell 批量替换文件

    Powershell 批量替换文件 ##作者:Xiongpq ##时间:2015-06-10 18:50 ##版本:2.0 ##源文件目录 ##源文件目录的所有文件都会覆盖目标目录的同名文件,源文件目 ...

  8. 解决win7访问不了局域网共享文件

    1.确认链接 2.确认服务TCP/IP NetBIOS Helper 启动 3.secpol.msc 确认 本地策略->用户权限分配 如图

  9. PA

    [题目描述] 汉诺塔升级了:现在我们有?个圆盘和?个柱子,每个圆盘大小都不一样, 大的圆盘不能放在小的圆盘上面,?个柱子从左到右排成一排.每次你可以将一 个柱子上的最上面的圆盘移动到右边或者左边的柱子 ...

  10. cf584a(水题)

    题意是输出一个能被t整除的n位数... 思路很简单,输出t和n-1个0即可.当然,还需要特判一下t为1,n为10的情况.. 代码如下: #include <bits/stdc++.h> u ...