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服务没有停止进程,确保停止重新再生成。

posted on 2009-06-26 15:06  叶子文文  阅读(675)  评论(2编辑  收藏  举报