windows 服务
一、创建一个Windows Service
1)创建Windows Service项目


2)对Service重命名
将Service1重命名为你服务名称,这里我们命名为ServiceTest。
二、创建服务安装程序
1)添加安装程序


之后我们可以看到上图,自动为我们创建了ProjectInstaller.cs以及2个安装的组件。
2)修改安装服务名
右键serviceInsraller1,选择属性,将ServiceName的值改为ServiceTest。

3)修改安装权限
右键serviceProcessInsraller1,选择属性,将Account的值改为LocalSystem。

三、写入服务代码
1)打开ServiceTest代码
右键ServiceTest,选择查看代码。
2)写入Service逻辑
添加如下代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Data.SqlClient; namespace WindowsService
{
public partial class Service1 : ServiceBase
{
System.Timers.Timer timer1 = new System.Timers.Timer(); public Service1()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Start.");
} timer1.Interval = ;
timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Tick);
timer1.Enabled = true;
timer1.Start(); } protected override void OnStop()
{
this.timer1.Stop();
} private void timer1_Tick(object sender, EventArgs e)
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "time.");
} SqlConnection conn = new SqlConnection("server=127.0.0.1;database=test;uid=sa;pwd=mfm123");
SqlCommand comm = new SqlCommand("insert into tb1(a,b)values ('111',11)", conn);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
}
}
四、安装
cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
InstallUtil.exe 路径\WindowsServiceTest.exe
net start ServiceTest \\这个是我们的服务名
为了方便,我们可以把这段代码放到 .bat 文件里直接执行就可以了!
执行就安装和启动了我们刚才建立的 windows 服务了!
五、卸载
如果我们想要删除,卸载这个服务的话,我们可以执行这段代码
cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
InstallUtil.exe /u 路径\WindowsServiceTest.exe
六、调试Windows Service
1)安装并运行服务
2)附加进程


3)在代码中加入断点进行调试

windows 服务的更多相关文章
- 基于SignalR实现B/S系统对windows服务运行状态的监测
通常来讲一个BS项目肯定不止单独的一个BS应用,可能涉及到很多后台服务来支持BS的运行,特别是针对耗时较长的某些任务来说,Windows服务肯定是必不可少的,我们还需要利用B/S与windows服务进 ...
- C#创建、安装、卸载、调试Windows Service(Windows 服务)的简单教程
前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...
- 玩转Windows服务系列汇总
玩转Windows服务系列汇总 创建Windows服务 Debug.Release版本的注册和卸载及其原理 无COM接口Windows服务启动失败原因及解决方案 服务运行.停止流程浅析 Windows ...
- 玩转Windows服务系列——给Windows服务添加COM接口
当我们运行一个Windows服务的时候,一般情况下,我们会选择以非窗口或者非控制台的方式运行,这样,它就只是一个后台程序,没有界面供我们进行交互. 那么当我们想与Windows服务进行实时交互的时候, ...
- 玩转Windows服务系列——使用Boost.Application快速构建Windows服务
玩转Windows服务系列——创建Windows服务一文中,介绍了如何快速使用VS构建一个Windows服务.Debug.Release版本的注册和卸载,及其原理和服务运行.停止流程浅析分别介绍了Wi ...
- 玩转Windows服务系列——Debug、Release版本的注册和卸载,及其原理
Windows服务Debug版本 注册 Services.exe -regserver 卸载 Services.exe -unregserver Windows服务Release版本 注册 Servi ...
- C# 开发windows服务的一些心得
最近在做一个windows服务的项目,发现并解决了一些问题,拿出来和大家分享一下,以下windows服务简称“服务” 文章会在适合时间更新,因为朋友们在不断提出新的意见或思路,感谢-.- 1.服务如何 ...
- 使用topshelf包装redis为windows服务
Redis服务端目前用的是控制台程序运行,部署的时候能作为windows服务后台运行感觉更好.找到一篇文章Running Redis as a Windows Service,利用win ...
- 编写Windows服务疑问1:操作过程
Windows 服务开发平时不太受人关注,毕竟那是高大上的项目类型,平常需求也用不上,很多老掉牙的家伙也只知有WinForm,仍不知有WPF,更别说Windows 服务了,正如陶渊明所写的,“不知有汉 ...
- C# windows服务制作(包括安装及卸载)
开篇语 因工作内容需要做一个windows服务,此前并没有相关经验,所以做了一个demo来跑跑这个梗(高手跳过,需要的来踩)- 效果如下:打开服务,可以找到我们新增的一个windows服务,这个dem ...
随机推荐
- split,slice,splice,replace的用法
split()方法用于把一个字符串分割成字符串数组 str.split("字符串/正则表达式从该参数制定额地方分割str",可选,可指定返回数组的最大长度,如果没设置参数,整个字符 ...
- 信号量 semaphore 和 @synchronized 的运用
1. //创建全局队列 dispatch_queue_t queue = dispatch_get_global_queue(0, 0); //创建信号量 dispatch_semaphore_t s ...
- music player界面
public class SoundPlayerGUI extends JFrame implements ChangeListener, ActionListener { private stati ...
- catalog备份数据库及RMAN存储脚本
环境说明: 提前配置好两个库的监听与tnsnames.oraIP:10.100.25.13 为目标数据库 IP:10.100.25.14 为恢复目录数据库(catalog database) 以下操 ...
- WAMPSERVER PHP版本5.3的降到 5.2?
在这里下载5.2版本PHP,http://sourceforge.net/projects/wampserver/files/WampServer%202%20-%20Extensions/PHP/下 ...
- Python>>>创建一个简单的3D场景
首先安装PyOpengl pip install PyOpenGL PyOpenGL_accelerate
- 安装和配置CentOS时钟同步服务
Type the following command to install ntp: # yum install -y ntp Turn on service: # chkconfig ntpd on ...
- MyBatis学习(一)、MyBatis简介与配置MyBatis+Spring+MySql
一.MyBatis简介与配置MyBatis+Spring+MySql 1.1MyBatis简介 MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架.MyBatis 摒除了大部分的J ...
- 开发语言性能对比,C++、Java、Python、LUA、TCC
一直想做开发语言性能对比,刚好有时间都做了给大家参考一下, 编译类:C++和Java表现还不错 脚本类:TCC脚本动态运行C语言,性能比其他脚本快好多... 想玩TCC的同学下载测试包,TCC目录下修 ...
- MySQL服务 - MySQL程序的配置文件、参数、变量查看
查看配置文件及读取顺序 MySQL的配置文件以.cnf结尾,可能会有多个,而不同版本的MySQL程序的读取配置文件的路径也都不同,要想获取MySQL读取配置文件的顺序可以通过以下指令查看: shell ...