.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 ...
随机推荐
- SQL Server 2005 系统数据介绍:dm_exec_connections
原文:SQL Server 2005 系统数据介绍:dm_exec_connections 转载:http://msdn.microsoft.com/zh-cn/library/ms181509(SQ ...
- 【Git】windows上git命令中文乱码的问题
windows上git命令中文乱码的问题解决 1.打开git bash快捷方式启动 2.右键 options 3.进入text选项卡,选中中文 和UTF-8 4.应用 测试[中文正常显示] 尝试打开文 ...
- 【Mybatis】mybatis查询报错org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'areaName' in 'class java.lang.String'
mybatis查询报错: Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for pro ...
- [置顶]
kubernetes资源对象--Horizontal Pod Autoscaling(HPA)
概念 HPA全称Horizontal Pod Autoscaling,即pod的水平自动扩展.自动扩展主要分为两种,其一为水平扩展,针对于实例数目的增减:其二为垂直扩展,即单个实例可以使用的资源的增减 ...
- IE常见BUG总结(持续更新)
ie6~7下display:inline-block无效 解决方案:需要hack触发hasLayout 1 //IE6.7中内联元素(如span)触发layout属性后, 它的行为和标准中的 inli ...
- 如何看一个VC工程具体是什么工程?
VC6等可以创建MFC, Win32等工程,拿到一个工程,怎么判断是什么工程呢? VC6全文检索看看有没有theApp 如果有一般就是MFC的 (VS?)看看工程设置,入口点函数写的是什么,/subs ...
- 有效的web安全信息源
杂志:hackcto ,书安 乌云知识库,91ri.org,安全脉搏(http://www.secpulse.com/) 乌云公开漏洞,乌云热点漏洞,90sec 内网渗透找90.web注入找习科, 已 ...
- html 后台页面布局
<!DOCTYPE html> <html lang="en"> <head> <title></title> < ...
- red5截屏
在red5-web.xml添加 <bean id="rtmpSampleAccess" class="org.red5.server.stream.RtmpSamp ...
- 升级滑动销毁activity,随着手势的滑动而滑动的效果
文章开头先注明本滑动销毁是对 http://blog.csdn.net/xiaanming/article/details/20934541 这篇博客的内容进行一个小小的改动 添加向左滑动打开另外一个 ...