服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#编写Windows服务程序详细步骤详解(图文)

C#编写Windows服务程序详细步骤详解(图文)

2022-01-22 21:09C#教程网 C#

本文介绍了如何用C#创建、安装、启动、监控、卸载简单的Windows Service 的内容步骤和注意事项,需要的朋友可以参考下

一、创建一个windows service

1)创建windows service项目

C#编写Windows服务程序详细步骤详解(图文)

C#编写Windows服务程序详细步骤详解(图文)

2)对service重命名

将service1重命名为你服务名称,这里我们命名为servicetest。

二、创建服务安装程序

1)添加安装程序

C#编写Windows服务程序详细步骤详解(图文)

C#编写Windows服务程序详细步骤详解(图文)

之后我们可以看到上图,自动为我们创建了projectinstaller.cs以及2个安装的组件。

2)修改安装服务名

右键serviceinsraller1,选择属性,将servicename的值改为servicetest。

C#编写Windows服务程序详细步骤详解(图文)

3)修改安装权限

右键serviceprocessinsraller1,选择属性,将account的值改为localsystem。

C#编写Windows服务程序详细步骤详解(图文)

三、写入服务代码

1)打开servicetest代码

右键servicetest,选择查看代码。

2)写入service逻辑

添加如下代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.diagnostics;
using system.linq;
using system.serviceprocess;
using system.text;
namespace windowsservicetest
{
    public partial class servicetest : servicebase
    {
        public servicetest()
        {
            initializecomponent();
        }
        protected override void onstart(string[] args)
        {
            using (system.io.streamwriter sw = new system.io.streamwriter("c:\\log.txt", true))
            {
                sw.writeline(datetime.now.tostring("yyyy-mm-dd hh:mm:ss ") + "start.");
            }
        }
        protected override void onstop()
        {
            using (system.io.streamwriter sw = new system.io.streamwriter("c:\\log.txt", true))
            {
                sw.writeline(datetime.now.tostring("yyyy-mm-dd hh:mm:ss ") + "stop.");
            }
        }
    }
}

这里我们的逻辑很简单,启动服务的时候写个日志,关闭的时候再写个日志。

四、创建安装脚本

在项目中添加2个文件如下(必须是ansi或者utf-8无bom格式):

1)安装脚本install.bat

%systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe windowsservicetest.exe

net start servicetest

sc config servicetest start= auto

2)卸载脚本uninstall.bat

%systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe /u windowsservicetest.exe

3)安装脚本说明

第二行为启动服务。

第三行为设置服务为自动运行。

这2行视服务形式自行选择。

4)脚本调试

如果需要查看脚本运行状况,在脚本最后一行加入pause

五、在c#中对服务进行控制

0)配置目录结构

简历一个新wpf项目,叫windowsservicetestui,添加对system.serviceprocess的引用。

在windowsservicetestui的bin\debug目录下建立service目录。

将windowsservicetest的生成目录设置为上面创建的service目录。

生成后目录结构如下图

C#编写Windows服务程序详细步骤详解(图文)

1)安装

安装时会产生目录问题,所以安装代码如下:

?
1
2
3
4
5
6
7
8
string currentdirectory = system.environment.currentdirectory;
system.environment.currentdirectory = currentdirectory + "\\service";
process process = new process();
process.startinfo.useshellexecute = false;
process.startinfo.filename = "install.bat";
process.startinfo.createnowindow = true;
process.start();
system.environment.currentdirectory = currentdirectory;

2)卸载

卸载时也会产生目录问题,所以卸载代码如下:

?
1
2
3
4
5
6
7
8
9
string currentdirectory = system.environment.currentdirectory;
 
system.environment.currentdirectory = currentdirectory + "\\service";
process process = new process();
process.startinfo.useshellexecute = false;
process.startinfo.filename = "uninstall.bat";
process.startinfo.createnowindow = true;
process.start();
system.environment.currentdirectory = currentdirectory;

3)启动

代码如下:

?
1
2
3
using system.serviceprocess;
servicecontroller servicecontroller = new servicecontroller("servicetest");
servicecontroller.start();

4)停止

?
1
2
3
servicecontroller servicecontroller = new servicecontroller("servicetest");
if (servicecontroller.canstop)
servicecontroller.stop();

5)暂停/继续

?
1
2
3
4
5
6
7
servicecontroller servicecontroller = new servicecontroller("servicetest");
if (servicecontroller.canpauseandcontinue){
if (servicecontroller.status == servicecontrollerstatus.running)
servicecontroller.pause();
else if (servicecontroller.status == servicecontrollerstatus.paused)
servicecontroller.continue();
}

6)检查状态

?
1
2
3
servicecontroller servicecontroller = new servicecontroller("servicetest");
 
string status = servicecontroller.status.tostring();

六、调试windows service

1)安装并运行服务

2)附加进程

C#编写Windows服务程序详细步骤详解(图文)C#编写Windows服务程序详细步骤详解(图文)

3)在代码中加入断点进行调试

C#编写Windows服务程序详细步骤详解(图文)

七、总结

本文对windows service的上述配置都未做详细解释,但是按上述步骤就可以制作可运行的windows service,从而达到了工作的需求。

延伸 · 阅读

精彩推荐
  • C#聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题,新手速来围观,一个通俗易懂的例子帮助大家更好的理解C#接口问题,感兴趣的小伙伴们可以参考一下...

    zenkey7072021-12-03
  • C#C#直线的最小二乘法线性回归运算实例

    C#直线的最小二乘法线性回归运算实例

    这篇文章主要介绍了C#直线的最小二乘法线性回归运算方法,实例分析了给定一组点,用最小二乘法进行线性回归运算的实现技巧,具有一定参考借鉴价值,需要...

    北风其凉8912021-10-18
  • C#C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    这篇文章主要介绍了C#实现的文件操作封装类,结合完整实例形式分析了C#封装文件的删除,移动,复制,重命名等操作相关实现技巧,需要的朋友可以参考下...

    Rising_Sun3892021-12-28
  • C#c#学习之30分钟学会XAML

    c#学习之30分钟学会XAML

    一个界面程序的核心,无疑就是界面和后台代码,而xaml就是微软为构建应用程序界面而创建的一种描述性语言,也就是说,这东西是搞界面的...

    C#教程网8812021-12-10
  • C#C#基础之泛型

    C#基础之泛型

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。接下来通过本文给大家介绍c#基础之泛型,感兴趣的朋友一起学习吧...

    方小白7732021-12-03
  • C#浅谈C# winForm 窗体闪烁的问题

    浅谈C# winForm 窗体闪烁的问题

    下面小编就为大家带来一篇浅谈C# winForm 窗体闪烁的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网7962021-12-21
  • C#C# 后台处理图片的几种方法

    C# 后台处理图片的几种方法

    本篇文章主要介绍了C# 后台处理图片的几种方法,非常具有实用价值,需要的朋友可以参考下。...

    IT小伙儿10162021-12-08
  • C#Unity3D UGUI实现缩放循环拖动卡牌展示效果

    Unity3D UGUI实现缩放循环拖动卡牌展示效果

    这篇文章主要为大家详细介绍了Unity3D UGUI实现缩放循环拖动展示卡牌效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    诗远3662022-03-11