VS 2008 打包Windows 服务

1 建立一个控制台程序:

代码很简单就是在C盘上写一个文本文件。看代码:

 

 1 namespace ConsoleApplication
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             CreateFile();
 8         }
 9 
10         public static void CreateFile()
11         {
12             StreamWriter sw = File.CreateText(@"C:\ThisCreatedByWindowsService.txt");
13             sw.Close();
14         }
15     }
16 }

 

 

2 我们的目的是将 上面的生成的控制台程序 "ConsoleApplication.exe" 打包成一个 Winsdows Service。接下来我们就在建议一个Windows Service 项目。

我们添加一个名为 TestService.cs的服务类.并在设计界面 点击右键 选择 “Add Install”。

此时,系统会自动生成一个 ProjectInstall.cs类。 下面我们来设置一些属性(直接看图吧,不多说了):

我们在这里设置 Account 为 "LocalSystem",因为我懒的管理权限 哈哈..

下面要复写 ServiceBase 类的 OnStart 方法。(Program 的访问权限得修改一下)

 

 1 partial class TestService : ServiceBase
 2     {
 3         public TestService()
 4         {
 5             InitializeComponent();
 6         }
 7 
 8         protected override void OnStart(string[] args)
 9         {
10              ConsoleApplication.Program.CreateFile();
11         }
12 
13         protected override void OnStop()
14         {
15              
16         }
17     }

 

好了,现在只差给这个服务打包了。

3 建立一个 “Setup Project”。添加一个"Project output"。“Project” 选择给那刚才的 Windows Service 项目。

 

然后添加 "Custom Actions".这里为 Install,Uninstall 添加。这个步骤必须做否则服务不会被安装和卸载。

编译成功后,我们就基本达到了目的。服务被安装上了:

等等,感觉不老好的... 对!没有自动启动呀!要解决这个问题 我们就得对 “ProjectInstaller ”类下手了:

 

 

 1 [RunInstaller(true)]
 2     public partial class ProjectInstaller : Installer
 3     {
 4         public ProjectInstaller()
 5         {
 6             InitializeComponent();
 7 
 8             this.serviceInstaller1.AfterInstall += new InstallEventHandler(serviceInstaller_AfterInstall);
 9             this.serviceInstaller1.AfterRollback += new InstallEventHandler(serviceInstaller_AfterRollback);
10             this.serviceInstaller1.AfterUninstall += new InstallEventHandler(serviceInstaller_AfterUninstall);
11 
12             this.serviceInstaller1.BeforeInstall += new InstallEventHandler(serviceInstaller_BeforeInstall);
13             this.serviceInstaller1.BeforeRollback += new InstallEventHandler(serviceInstaller_BeforeRollback);
14             this.serviceInstaller1.BeforeUninstall += new InstallEventHandler(serviceInstaller_BeforeUninstall);
15         }
16 
17         void serviceInstaller_BeforeRollback(object sender, InstallEventArgs e)
18         {
19             StopService();
20         }
21 
22         void serviceInstaller_BeforeInstall(object sender, InstallEventArgs e)
23         {
24             StopService();
25         }
26 
27         void serviceInstaller_AfterUninstall(object sender, InstallEventArgs e)
28         {
29             StopService();
30         }
31 
32         void serviceInstaller_AfterRollback(object sender, InstallEventArgs e)
33         {
34             StopService();
35         }
36 
37         void serviceInstaller_BeforeUninstall(object sender, InstallEventArgs e)
38         {
39             StopService();
40         }
41 
42         void serviceInstaller_AfterInstall(object sender, InstallEventArgs e)
43         {
44             StartService();
45         }
46 
47         void StartService()
48         {
49             System.ServiceProcess.ServiceController serverContorler = new ServiceController(this.serviceInstaller1.ServiceName);
50 
51             if (serverContorler != null)
52             {
53                 if (serverContorler.Status != ServiceControllerStatus.Running)
54                 {
55                     serverContorler.Start();
56                     serverContorler.Dispose();
57                 }
58 
59             }
60 
61         }
62 
63         void StopService()
64         {
65             System.ServiceProcess.ServiceController serverContorler = new ServiceController(this.serviceInstaller1.ServiceName);
66 
67             if (serverContorler != null)
68             {
69                 if (serverContorler.CanStop)
70                 {
71                     serverContorler.Stop();
72                     serverContorler.Dispose();
73                 }
74 
75             }
76         }
77     }

 

自己启动了! C盘上的文件也建立起来了!

 

补充:

1. 调试安装程序包:在 ProjectInstaller 类里面你想调试的方法里(例如:OnAfterInstall)添加:

System.Diagnostics.Debugger.Launch();

2. 制定 Customer Action:

    选择安装项目->进入"Custom Action"视图-> 以Install为例,选中"Install"->右键 "Add Custom Action"->选择项目输出->在 "CustomActionData"

    中填写 /pwd=[EDITA1] /targetdir="[TARGETDIR]\"  说明:[EDITA1]是自添加的对话框的 textbox edit1的Property “[TARGETDIR]” 是系统参数

    值为安装目录.

 

    在 ProjectInstall类中 获得参数代码:

     InstallContext installContext = this.Context;

     string password = installContext.Parameters["pwd"];

 

 源码

posted @ 2010-05-10 15:39  刘雨赣  阅读(3769)  评论(9编辑  收藏  举报