教学思路WinForms之 windows服务

简介:
1.新建windows服务项目:
    在VSIDE中点击新建项目中VC#下--windows中--windows服务--改变项目命名,选中项目路径--确定。
2.介绍文件构成:
     创建后项目中会自动带有Program.cs和Service1.cs,Service1是没有图形化界面的,这是因为windows服务基本都是没有界面的,系统运行后就加载的这些服务程序,系统启动时 便开始运行,不需要用户登录,windows服务需要安装并在注册表中进行注册。
     点击Service1界面右键“查看代码”或点击“单击此处切换到代码视图”,代码中会出现2个重要的方法,一个 protected override void OnStart(string[] args){}方法,用于处理windows服务启动后将运行的程序。一个 protected override void OnStop(){}方法,用于处理windows服务停止后将运行的程序。
3.编写windows服务程序
下面我们就来编写一个windows服务,当开启这个服务时,将在C盘下记录系统运行时间和操作信息,当结束服务时,记录结束的时间。
代码如下:
 
 1  using  System;
 2  using  System.Collections.Generic;
 3  using  System.ComponentModel;
 4  using  System.Data;
 5  using  System.Diagnostics; // 进程的命名空间
 6  using  System.Linq;
 7  using  System.ServiceProcess;
 8  using  System.Text;
 9  using  System.IO; // 引入文件流的命名空间
10 
11  namespace  WindowsService1
12  {
13       public   partial   class  Service1 : ServiceBase
14      {
15           public  Service1()
16          {
17              InitializeComponent();
18          }
19      
20 
21           int  compuTime;
22           // 启动时执行
23           protected   override   void  OnStart( string [] args)
24          {
25              FileStream fs  =   new  FileStream( @" c:\Log.Txt " ,FileMode.OpenOrCreate,FileAccess.Write);
26              StreamWriter sw  =   new  StreamWriter(fs);
27              sw.BaseStream.Seek( 0 , SeekOrigin.End);
28              sw.WriteLine( " 开始时间: "   +  DateTime.Now.ToString());
29 
30 
31              compuTime  =  Environment.TickCount;
32 
33               long  curTickValue  =  Environment.TickCount;
34               long  difference  =  curTickValue  -  compuTime;
35 
36               long  computerHours, computerMinutes, computerSeconds;
37               long  applicationHours, applicationMinutes, applicationSeconds;
38 
39           
40              computerHours  =  (curTickValue  /  ( 3600   *   999 ))  %   24 ;
41              computerMinutes  =  (curTickValue  /  ( 60   *   999 ))  %   60 ;
42              computerSeconds  =  (curTickValue  /   999 %   60 ;
43 
44              applicationHours  =  (difference  /  ( 3600   *   999 ))  %   24 ;
45              applicationMinutes  =  (difference  /  ( 60   *   999 ))  %   60 ;
46              applicationSeconds  =  (difference  /   999 %   60 ;
47 
48              sw.WriteLine (String.Format( " 本计算机已运行了 {0} 小时 {1} 分 {2} 秒 " , computerHours.ToString(), computerMinutes.ToString(),computerSeconds.ToString()));
49 
50 
51              sw.WriteLine (String.Format( " 本应用程序已运行了 {0} 小时 {1} 分 {2} 秒 " ,
52              applicationHours.ToString(), applicationMinutes.ToString(),
53              applicationSeconds.ToString()));
54 
55              sw.WriteLine( " 机器名称: "   +  Environment.MachineName);
56              sw.WriteLine( " 系统版本: "   +  Environment.OSVersion);
57              sw.WriteLine( " 系统路径: "   +  Environment.SystemDirectory);
58              sw.WriteLine( " 系统用户名: "   +  Environment.UserName);
59              sw.WriteLine( " 系统.Net版本: "   +  Environment.Version);
60              
61 
62              sw.Flush();
63              fs.Close();
64          }
65 
66           // 停止
67           protected   override   void  OnStop()
68          {
69              FileStream fs  =   new  FileStream( @" c:\Log.Txt " , FileMode.OpenOrCreate, FileAccess.Write);
70              StreamWriter sw  =   new  StreamWriter(fs);
71              sw.BaseStream.Seek( 10 ,SeekOrigin.End);
72              sw.WriteLine( " 结束时间: "   +  DateTime.Now.ToString());
73              sw.WriteLine( " ************************************ " );
74              sw.Flush();
75              sw.Close();
76          }
77      }
78  }
79 
   
4.添加安装程序
       在Service1视图界面点击右键“添加安装程序”,将会生成两个组件,分别设置安装组件的属性。
       serviceProcessInstaller1组件修改一个属性
                                              Account属性改为:LocalSystem
       serviceInstaller1组件修改三个属性
                                              Description属性可以填写描述windows服务的内容,如我的windows服务;        
                                              DisplayName 属性填写windows服务的服务名称 如MywindowsService;
                                              StartType属性改为Automatic;//手工对服务进行操作
5.生成项目
           生成项目调试错误,直至提示生成成功,到项目所在的文件夹下确认以及生成exe可执行文件,如我的项目创建在D:\msd0902练习\WindowsService1\bin\Debug下,以及成功生成了WindowsService1.exe。
6.在系统中安装windows服务。
          在开始菜单打开.Net Framework SDK或VS Tools中的命令行程序将出现黑屏下的命令行语句,在VC>后写下installutill命令,然后打空格,再写上你的windows服务的生成目录及“\”和windows服务的执行命令的文件名,最后按回车键。
          如 installutil  "D:\msd0902练习\WindowsService1\bin\Debug\WindowsService1.exe" 
         提示成功后,及表示注册成功。
7.开启服务,查看程序运行效果。
        在控制面板-- 管理工具--- 服务中找到windows服务的服务名称如MywindowsService,右键启动服务,启动后,可以到C盘下找到Log.txt文件,如果你没有找到,说明你前面的过程有错误,请重新进行一遍。
        打开文件后,执行的效果是:
本计算机已运行了 1 小时 16 分 44 秒
本应用程序已运行了 0 小时 0 分 0 秒
机器名称:977D914A3540495
系统版本:Microsoft Windows NT 5.1.2600 Service Pack 3
系统路径:C:\WINDOWS\system32
系统用户名:LOCAL SERVICE
系统.Net版本:2.0.50727.1433

8. 停止服务,查看程序运行效果
               在服务中找到windows服务的服务名称如MywindowsService,右键停止服务,停止后,查看c盘下Log.txt文件,打开文件后,执行的效果是:
本计算机已运行了 1 小时 16 分 44 秒
本应用程序已运行了 0 小时 0 分 0 秒
机器名称:977D914A3540495
系统版本:Microsoft Windows NT 5.1.2600 Service Pack 3
系统路径:C:\WINDOWS\system32
系统用户名:LOCAL SERVICE
系统.Net版本:2.0.50727.1433
          结束时间:2009-6-26 14:14:53   
*********************************  
 
9.卸载windows服务
               无用的windows服务,我们应该停止服务,以免占内存,现在将我们的windows服务在开始菜单打开.Net Framework SDK或VS Tools中的命令行程序将出现黑屏下的命令行语句,在VC>后打入installutil  "D:\msd0902练习\WindowsService1\bin\Debug\WindowsService1.exe"   /u 然后按回车键,提示卸载成功,我们的windows服务就成功卸载了。
 
10.修改windows服务
                 对已经生成过的windows服务代码进行修改时,首先要停止此windows服务,然后修改代码后,重新生成即可,如提示“…exe…正在使用”说明你的windows服务没有停止进程,确保停止重新再生成。
本文转自叶子文文博客51CTO博客,原文链接http://blog.51cto.com/leafwf/185797如需转载请自行联系原作者

叶子文文
相关文章
|
1月前
|
Java 数据库 C#
C#winforms实现windows窗体人脸识别
C#winforms实现windows窗体人脸识别
30 0
|
1月前
|
Linux Shell Windows
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
|
1月前
|
存储 安全 数据安全/隐私保护
Windows部署WebDAV服务并映射到本地盘符实现公网访问本地存储文件
Windows部署WebDAV服务并映射到本地盘符实现公网访问本地存储文件
269 0
|
2月前
|
Java Unix 应用服务中间件
使用java service wrapper把windows flume做成服务
使用java service wrapper把windows flume做成服务
|
21天前
|
人工智能 机器人 编译器
【C++】Windows端VS code中运行CMake工程(手把手教学)
【C++】Windows端VS code中运行CMake工程(手把手教学)
|
2月前
|
Windows
修改Windows服务的配置
修改Windows服务的配置
|
3月前
|
Arthas 监控 Java
Arthas 可以用于监控和诊断在 Windows 系统下部署的 Tomcat 服务
Arthas 可以用于监控和诊断在 Windows 系统下部署的 Tomcat 服务
175 2
|
26天前
|
Shell Windows
Windows服务器 开机自启动服务
Windows服务器 开机自启动服务
14 0
|
4月前
|
网络协议 安全 文件存储
Windows本地搭建WebDAV服务并使用内网穿透远程访问【无公网IP】
Windows本地搭建WebDAV服务并使用内网穿透远程访问【无公网IP】
|
4月前
|
监控 Linux 定位技术
Linux【环境部署 01】NTP时间服务器搭建及Linux+Windows客户端使用(一篇学会使用NTP服务)
Linux【环境部署 01】NTP时间服务器搭建及Linux+Windows客户端使用(一篇学会使用NTP服务)
662 0