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服务程序虽然是可执行的,但是它不像一般的可执行文件通过双击就 ...
随机推荐
- [codeforces 325]B. Stadium and Games
[codeforces 325]B. Stadium and Games 试题描述 Daniel is organizing a football tournament. He has come up ...
- nginx学习(二):初识配置文件
nginx的配置文件默认在nginx安装目录中的conf子目录中,主配置文件为nginx.conf, root@mgmserver conf]# pwd/usr/local/nginx/conf一.配 ...
- linux下cp目录时排除一个或者多个目录的方法
说明:/home目录里面有data目录,data目录里面有a.b.c.d.e五个目录,现在要把data目录里面除过e目录之外的所有目录拷贝到/bak目录中 系统运维 www.osyunwei.com ...
- poj 1664
http://poj.org/problem?id=1664 题目是中文的,一个递归的题目 把每一次的苹果分为两类 Ⅰ:所以盘子都放一个,然后其他的在随便放: Ⅱ:有一个盘子没有放苹果: 这样下去的话 ...
- iOS 拍照中加入GPS和具体地理位置
最近有一个需求,要求用手机拍个照片,并切需要拍摄时间,拍摄gps,拍摄具体街道信息. 首先要感谢PhotoGPSdemo的作者,你可以到这里下载demo http://www.cocoachina.c ...
- Java内存分配和内存管理
首先是概念层面的几个问题: Java中运行时内存结构有哪几种? Java中为什么要设计堆栈分离? Java多线程中是如何实现数据共享的? Java反射的基础是什么? 然后是运用层面: 引用类型变量和对 ...
- springJDBC实现mysql简单分页
效果图:
- HDU1267 递推
下沙的沙子有几粒? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- 解决win7访问不了局域网共享文件
1.确认链接 2.确认服务TCP/IP NetBIOS Helper 启动 3.secpol.msc 确认 本地策略->用户权限分配 如图
- stsadm.exe
Usage: stsadm.exe -o <operation> [<parameters>] stsadm.exe -help [<operation>] Ope ...