.Net 开发Windows Service
1、首先创建一个Windows Service
2、创建完成后切换到代码视图,代码中默认有OnStart和OnStop方法执行服务开启和服务停止执行的操作,下面代码是详细解释:
using System;
using System.IO;
usingSystem.ServiceProcess;
using System.Text;
usingSystem.Timers;
namespaceTestService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override voidOnStart(string[] args)
{
//服务开启执行代码
StartDoSomething();
}
protected override void OnStop()
{
//服务结束执行代码
}
protected override void OnPause()
{
//服务暂停执行代码
base.OnPause();
}
protected override void OnContinue()
{
//服务恢复执行代码
base.OnContinue();
}
protected override void OnShutdown()
{
//系统即将关闭执行代码
base.OnShutdown();
}
private void StartDoSomething()
{
System.Timers.Timer timer = newSystem.Timers.Timer(10000); //间隔10秒
timer.AutoReset = true;
timer.Enabled = false; //执行一次
timer.Elapsed += newElapsedEventHandler(WriteSomething);
timer.Start();
}
private void WriteSomething(objectsource, System.Timers.ElapsedEventArgs e)
{
FileStream fs = null;
try
{
fs = newFileStream("d:/1.txt", FileMode.OpenOrCreate);
string strText = @"//实例化一个文件流--->与写入文件相关联
FileStream fs = newFileStream(sf.FileName, FileMode.Create);
//实例化一个StreamWriter-->与fs相关联
StreamWriter sw = newStreamWriter(fs);
//开始写入
sw.Write(this.textBox1.Text);
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
fs.Close();";
//获得字节数组
byte[] data = newUTF8Encoding().GetBytes(strText);
//开始写入
fs.Write(data, 0, data.Length);
//清空缓冲区、关闭流
fs.Flush();
fs.Close();
fs.Dispose();
}
catch
{
}
finally
{
if (fs != null)
{
fs.Close();
fs.Dispose();
}
}
}
}
}
3、然后切换到设计视图,右键点击下图中圈选的“添加安装程序”

4、选中下图第一个控件,点击F4,右边切换到属性视图;更改属性视图中的Account属性为LocalService(本地服务)

5、
选中上面第二个控件,点击F4,右边切换到属性视图。更改ServiceName为你自己喜欢的服务名称,记住不要和系统的冲突了哦~,亲!StartType默认为手动,你可以更改为自动
(Automatic)或禁用(Disabled)

6、编译项目,然后win+R输入cmd进入命令窗口。去对应.net版本下的目录中找到InstallUtil.exe,我项目采用的是 .net 2.0,故路径为C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

Cmd命令
installutil WindowsService_test.exe (安装Windows服务)
installutil /u WindowsService_test.exe (卸载Windows服务)
.Net 开发Windows Service的更多相关文章
- .NET开发Windows Service程序 - Topshelf
在实际项目开发过程中,会经常写一些类似定时检查,应用监控的应用.这类应用在windows平台通常都会写成window service程序. 在百度上搜索一下'c#开发windows service', ...
- 使用C#编程语言开发Windows Service服务
转载-https://www.cnblogs.com/yubao/p/8443455.html Create Windows Service project using Visual Studio C ...
- C# Windows service 开发笔录
本文将详细图解,开发Windows service的过程. 功能:数据库查询数据后,经过处理,每天定时发送邮件. 一.WinForm调试 1.新建Windows service项目 2.新建WinFo ...
- Python3 写Windows Service服务程序
用Python开发Windows Service,用Python来做这个事情必须要借助第三方模块pywin32,下载路径:https://pypi.org/project/pywin32/#files ...
- Windows Service的转换与部署
开发Windows Service,可能会碰到以下两种情况. 1. 直接开发一个Windows Service 网上有很多教程,可以参考这个: http://www.cnblogs.com/sorex ...
- Windows Service 开发,安装与调试
Visual Studio.net 2010 Windows Service 开发,安装与调试 本示例完成一个每隔一分钟向C:\log.txt文件写入一条记录为例,讲述一个Windows Servic ...
- 如何托管ASP.NET Core应用到Windows Service中
(此文章同时发表在本人微信公众号"dotNET开发经验谈",欢迎右边二维码来关注.) 题记:正在构思一个中间件的设计,考虑是否既可以使用最新的技术,也可以兼顾传统的部署模式.所以有 ...
- 使用Visual Studio 2015 Community 开发windows服务
昨天研究在.NET下开发Windows服务程序,期间遇到一些小问题,这里仅将自己的开发过程和需要注意的地方写下和广大网友分享…… 1.基础 Windows服务是指系统启动时能够自己运行的程序.W ...
- 使用Python写Windows Service服务程序
1.背景 如果你想用Python开发Windows程序,并让其开机启动等,就必须写成windows的服务程序Windows Service,用Python来做这个事情必须要借助第三方模块pywin32 ...
随机推荐
- c++中resize这个函数怎么用
c++中序列式容器的一个共性函数, vv.resize(int n,element)表示调整容器vv的大小为n,扩容后的每个元素的值为element,默认为0 resize()会改变容器的容量和当前元 ...
- 【Linux】CentOS7上安装搜狗输入法
找到一篇资料,提供安装搜狗输入法的方法,在http://www.cnblogs.com/Yiutto/p/6204085.html[也是本文的参考原地址] 请大家自行下载fcitx.tar.gz 1. ...
- UITableView的HeaderView和FooterView
header通过下面两个代理方法设置 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSIntege ...
- react数组key的唯一性
1.不要使用数组的index索引作为key 2.在相邻的元素间,一定确保key的唯一性,如果出现了相同的 key,会抛出一个 Warning,告诉相邻组件间有重复的 key 值.并且只会渲染第一个重复 ...
- thrift.transport.TTransport.TTransportException: Could not start SASL: Error in sasl_client_start (-4) SASL(-4): no mechanism available: No worthy mechs found
thrift.transport.TTransport.TTransportException: Could not start SASL: Error in sasl_client_start (- ...
- 【共享单车】—— React后台管理系统开发手记:权限设置和菜单调整(未完)
前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...
- 最简单简洁高效的Json数据解析
一.无图无真相 二.主要代码 1.导入jar包 拷贝fastjson.jar包到projectlibs包下 2.封装工具类JsonUtil.java package com.example.parse ...
- node - 写返回mime类型
何为mime类型? mime.json { ".323":"text/h323" , ".3gp":"video/3gpp&qu ...
- Linux下实现文件双向同步
修改的地方时:私钥和公约处. 其次 vi /root/.unison/default.prf 这个文件在这个位置 root = /tmp/test root = ssh://root@10.10.3. ...
- 【Excle】在重复数据中对日期排序并查询最新的一条记录
现在存在以下数据: 需要查询出以下数据 姓名 日期 张三 2017-12-14 李四 2017-12-16 在E1中写入以下公式:=IF(D2=MAX(IF($C$ ...