初识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.服务如何 ...
随机推荐
- 2018/1/27 Zookeeper实现分布式锁
public class DistributedClient { // 超时时间 private static final int SESSION_TIMEOUT = 5000; // zookeep ...
- iis发布网站问题-由于权限不足而无法读取配置文件,无法访问请求的页面
错误一: HTTP Error 500.19 - Internal Server Error 配置错误: 不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况.锁定是默认设置的 (o ...
- VS2012如何调试JS
下面的操作步骤描述了怎样利用vs.net中的调试器来调试javascript: 1,首先,要让你的ie允许调试脚本,具体步骤如下: 打开ie->工具菜单->inter选项->高 ...
- 在CentOS上部署多节点Citus集群
1 在所有节点执行以下步骤 Step 01 添加Citus Repostory # Add Citus repository for package manager curl https://inst ...
- [Swift]UIKit学习之警告框:UIAlertController和UIAlertView
Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) T ...
- PHPUnit-附录 A. 断言 (assert)
[http://www.phpunit.cn/manual/5.7/zh_cn/appendixes.assertions.html] 本附录列举可用的各种断言方法. assertArrayHasKe ...
- 工作中代码笔记 -- adb命令篇
1.抓log方法 (bat文件) mkdir D:\logcatset /p miaoshu=请描述操作:adb logcat -v threadtime > D:\logcat\%miaosh ...
- ARM平台的虚拟化介绍
本篇博文主要介绍虚拟化的基本思想以及在arm平台如何做虚拟化,arm提供的硬件feature等等. 虚拟化技术简介 虚拟化技术 虚拟化是一个概念,单从这个概念的角度来看,只要是用某一种物品去模拟另一种 ...
- Python基础——for/while循环
Python版本:3.6.2 操作系统:Windows 作者:SmallWZQ 上学期间,常常遇到这样的情景:为了惩罚学生,老师会说:"XXX,你先去操场上跑10圈再回来继续反省.&qu ...
- Linux上查看用户名和组并把特定用户放到特定的组之下
cat /etc/passwd //查看所有的用户信息 cat /etc/passwd|grep 用户名 //查看某一个用户的信息 cat /etc/group ...