当我们需要一个程序长期运行,但是不需要界面显示时可以考虑使用Windows Service来实现。这篇博客将简单介绍一下如何创建一个Windows Service,安装/卸载Windows Service。

新建Windows Service项目:

删除自动生成的Service1.cs文件,新建WindowsService类,继承ServiceBase。

    class WindowsService : ServiceBase
{
public WindowsService()
{
this.ServiceName = "Test Windows Service";
this.EventLog.Log = "Application"; this.CanHandlePowerEvent = true;
this.CanHandleSessionChangeEvent = true;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.CanStop = true;
} #region
// 可以把需求实现代码放置在重写方法内
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
} protected override void OnStart(string[] args)
{
base.OnStart(args);
} protected override void OnStop()
{
base.OnStop();
} protected override void OnPause()
{
base.OnPause();
} protected override void OnContinue()
{
base.OnContinue();
} protected override void OnShutdown()
{
base.OnShutdown();
} protected override void OnCustomCommand(int command)
{
base.OnCustomCommand(command);
} protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
{
return base.OnPowerEvent(powerStatus);
} protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
base.OnSessionChange(changeDescription);
}
#endregion
}

新建WindowsServiceInstaller类,添加System.Configuration.Install引用,

    [RunInstaller(true)]
class WindowsServiceInstaller : Installer
{
public WindowsServiceInstaller()
{
ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new ServiceInstaller(); serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
serviceProcessInstaller.Username = null;
serviceProcessInstaller.Password = null; serviceInstaller.DisplayName = "CSharp Windows Service";
serviceInstaller.StartType = ServiceStartMode.Automatic; serviceInstaller.ServiceName = "Windows Service"; this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
}
}

编译项目,此时编译完的CSWindowsService.exe运行后提示:

我们需要通过installutil.exe来部署Windows Service。

以管理员身份运行Developer Command Prompt。部署命令:installutil /i 文件路径

例如:installutil /i D:\CSWindowsService.exe

卸载命令 installutil /u 文件路径。

到这里,一个简单的Windows Serive就讲完了。

关于Windows Service更多的内容,请参考MSDN文档。

感谢您的阅读,代码点击这里下载。

C# 创建Windows Service的更多相关文章

  1. C#创建Windows Service(Windows 服务)基础教程

    Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...

  2. 创建Windows Service

    基本参照使用C#创建Windows服务,添加了部分内容 目录 创建Windows Service 可视化管理Windows Service 调试 示例代码 创建Windows Service 选择C# ...

  3. C# 创建Windows Service(Windows服务)程序

    本文介绍了如何用C#创建.安装.启动.监控.卸载简单的Windows Service 的内容步骤和注意事项. 一.创建一个Windows Service 1)创建Windows Service项目 2 ...

  4. .NET 6学习笔记(2)——通过Worker Service创建Windows Service

    通过Visual Studio中的Windows Service模板,我么可以创建.NET Framework版本的Windows Service,网络上对此已有详细且丰富的各路教程.但在我们升级到. ...

  5. 目前.NET Core创建Windows Service比较好的一个开源框架:DasMulli.Win32.ServiceUtils

    新建一个.NET Core控制台程序,搜索并下载Nuget包:DasMulli.Win32.ServiceUtils GitHub 链接及使用指南 Write a windows service us ...

  6. 通过TopShelf简单创建windows service

    目前很多项目都是B/S架构的,我们经常会用到webapi.MVC等框架,实际项目中可能不仅仅是一些数据的增删改查,需要对数据进行计算,但是将计算逻辑放到api层又会拖累整个项目的运行速度,从而会写一些 ...

  7. 使用.net 创建windows service

    最近公司项目需要,写了个windows 服务,windows 服务的内容可以在VS 中新建一个"windows服务项目", (1)服务中的主要代码: public partial ...

  8. 【C#】C#创建Windows Service服务

    目录结构: contents structure [+] 创建Windows服务 配置 安装Windows服务 在Visual Studio中调试 常见问题 最近写了一个TCP连接的程序,由于这种通信 ...

  9. VS2010 创建 windows service 程序

    参考网上保护眼睛程序,自写程序如下. 1.创建一个名词为“CareEyeService”,类型为“WindowsService”的应用程序. 自动生成代码如下图: 2.修改ServiceCareEye ...

随机推荐

  1. SX学SX内容 笔记?

    某帖子笔记1 主要还是从三体吧某精品贴里看来的... 集合论 集合就是一堆东西...满足 1) 集合中的元素互异(即每种只有一个) 2) 集合中的元素无序(不是一个数组,集合中的元素没有显然的排序法则 ...

  2. iframe框架中用js改变父级Url

    <script type="text/javascript">        var path = window.location.href;//当前也面的跳转     ...

  3. ubuntu查看端口占用

    查看端口号 sudo netstat -ltnp | 结束进程 sudo kill pid

  4. win7桌面背景地址

    C:\Users\你的用户名\AppData\Local\Microsoft\Windows\Themes\Alaskan L\DesktopBackground

  5. 《转载》化繁为简 如何向老婆解释MapReduce?

    本文转载自http://server.zol.com.cn/329/3295529.html 昨天,我在Xebia印度办公室发表了一个关于MapReduce的演说.演说进行得很顺利,听众们都能够理解M ...

  6. 《oracle每天一练》Merge Into 语句代替Insert/Update在Oracle中的应用实战

    转载自窃破天道 动机: 想在Oracle中用一条SQL语句直接进行Insert/Update的操作. 说明: 在进行SQL语句编写时,我们经常会遇到大量的同时进行Insert/Update的语句 ,也 ...

  7. centos7下搭建git和gitlab版本库

    系统:centos7 安装依赖: yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-d ...

  8. Qt 改变图片大小

    void Setting_TabProduct::changeImageSize(int width,int height,QString imgFile) { QPixmap pixmap(imgF ...

  9. perl Can't use string Cxxx) as a symbol ref while "strict refs" in use at XXXX.pl错误

    今天写脚本遇到Can't use string ("bond2     Link encap:InfiniBand ") as a symbol ref while "s ...

  10. java sleep() 、yield()

    1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁.也就是说如果有synchronized同步快,其他线程仍然不能访问共享数据.注意该方 ...