初识Windows服务 C#
1.新建Windows服务

2.切换到代码视图,拷入如下代码
该服务以10S的间隔创建 d:/1.txt 文件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace WindowsServiceTest
{
publicpartialclassService1:ServiceBase
{
publicService1()
{
InitializeComponent();
}
protectedoverridevoidOnStart(string[] args)
{
//服务开启执行代码
StartDoSomething();
}
protectedoverridevoidOnStop()
{
//服务结束执行代码
}
protectedoverridevoidOnPause()
{
//服务暂停执行代码
base.OnPause();
}
protectedoverridevoidOnContinue()
{
//服务恢复执行代码
base.OnContinue();
}
protectedoverridevoidOnShutdown()
{
//系统即将关闭执行代码
base.OnShutdown();
}
privatevoidStartDoSomething()
{
System.Timers.Timer timer =newSystem.Timers.Timer(10000);//间隔10秒
timer.AutoReset=true;
timer.Enabled=false;//执行一次
timer.Elapsed+=newElapsedEventHandler(WriteSomething);
timer.Start();
}
privatevoidWriteSomething(object source,System.Timers.ElapsedEventArgs e)
{
FileStream fs =null;
try
{
fs =newFileStream("d:/1.txt",FileMode.OpenOrCreate);
string strText =@"以10秒的间隔重复创建该文件,若已有同名文件,则保持不变";
//获得字节数组
byte[] data =new UTF8Encoding().GetBytes(strText);
//开始写入
fs.Write(data,0, data.Length);
//清空缓冲区、关闭流
fs.Flush();
fs.Close();
fs.Dispose();
}
catch
{
}
finally
{
if(fs !=null)
{
fs.Close();
fs.Dispose();
}
}
}
}
}
3.添加安装程序并设置控件属性

2.将左上角第一个控件的Account属性设置为LocalService



4.编译项目
1.生成解决方案(Ctrl+Shift+B),编译完成后会生成对应的xxx.exe

2.找到系统里面InstallUtil的安装目录 例如 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe 实在找不到就用Everything吧
3.Win+R CMD cd 跳转到InstallUtil的安装路径,运行如下命令 InstallUtil.exe+空格+4.1生成的exe的目录
cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319
InstallUtil.exe E:\GitVSTest\WindowsServiceTest\WindowsServiceTest\bin\Debug\WindowsServiceTest.exe

5.启动服务

6.修改服务

7.卸载服务
初识Windows服务 C#的更多相关文章
- C#创建windows服务(一:初识windows服务)
一 . 服务简介 Microsoft Windows 服务(过去称为 NT 服务)允许用户创建可在其自身的 Windows 会话中长时间运行的可执行应用程序. 这些服务可在计算机启动时自动启动,可以暂 ...
- windows服务初识
参考网址1:http://www.vchome.net/dotnet/dotnetdocs/dotnet38.htm 参考网址2:http://zhidao.baidu.com/link?url=7- ...
- 基于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.服务如何 ...
随机推荐
- JDBC常见面试题
以下我是归纳的JDBC知识点图: 图上的知识点都可以在我其他的文章内找到相应内容. JDBC常见面试题 JDBC操作数据库的步骤 ? JDBC操作数据库的步骤 ? 注册数据库驱动. 建立数据库连接. ...
- IDEA的Maven依赖如何引入到External Libraries中
现象 在Apollo项目中,遇到了一个问题.当在Module的pom.xml中引入依赖: <dependency> <groupId>com.ctrip.framework.a ...
- 02-Nginx+MySQL+PHP7
[安装Nginx] #先安装如下包 yum install gcc gcc-c++ kernel-devel yum -y install pcre-devel openssl openssl-dev ...
- python学习:备份文档并压缩为zip格式
import os import time source = ['/root/notes'] target_dir = '/root/backup' if not os.path.exists(tar ...
- APACHE服务器出现No input file specified.解决方案
thinkcmf程序默认的.htaccess里面的规则: <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_F ...
- CSS布局(一) 盒子模型
一.盒子模型 标准盒子模型 从下图可以看到标准 w3c 盒子模型的范围包括 content.padding.border.margin,并且 content 部分不包含其他部分. 怪异盒子模型 从下图 ...
- ucore文件系统详解
最近一直在mooc上学习清华大学的操作系统课程,也算是复习下基本概念和原理,为接下来的找工作做准备. 每次深入底层源码都让我深感操作系统实现的琐碎,即使像ucore这样简单的kernel也让我烦躁不已 ...
- mac中配置jdk环境
- 对于文件File类型中的目录分隔符
当我们在使用File f=new File(path); 对于这个path来说是一个很痛苦的事情,在不同的系统上面的分隔符号是不一样的 在windows下面的话是一个"\"而在un ...
- 关于FFMPeg-PHP你必须要知道的
#PHP FFmpeg []( ...