[转贴]写自动安装的Windows服务(不需要安装命令InstallUtil)

Once you have your service written, you'll need to add an installer class. This should derive from System.Configuration.Install.Installer. In this class's constructor, you need to add a System.ServiceProcess.ServiceProcessInstaller to your Installers collection. This will install the process your service lives in. Set its Username and Password properties to match the username and password of the account you'd like the process to run as.


You'll also need to add an installer for the service itself. The class that does this is System.ServiceProcess.ServiceInstaller. The only thing you need to do to it before adding it to the Installers collection is to set the ServiceName property. IMPORTANT! This must match the ServiceName you used in your ServiceBase-derived class.


The code that does all this looks like this:


using System;
using System.Configuration.Install ;
using System.ServiceProcess ;
using System.ComponentModel ;


public class MyInstaller : Installer
{
  public MyInstaller ()
  {
     ServiceProcessInstaller spi = new ServiceProcessInstaller ();
     spi.Username = "ISENGARD// CheckURL ";
     spi.Password = "ihsxa9up";


     ServiceInstaller si = new ServiceInstaller ();
     si.ServiceName = " CheckURL ";


     this .Installers.Add ( spi );
     this .Installers.Add ( si );
  }
}


If you've done installers before, you know this is the same code that the wizard will write for you. The only difference is that InstallUtil (the usual way of doing things) relies on the presence of the [RunInstaller(true)] attribute on the installer class. Adding won't hurt, but we don't need it so I've left it out.


The next thing we need to do is get the classes we've inherited from to do all the work. You can put this next bit of code anywhere you like, but I sort of like the idea of my service being self-installing, so I've added code to my Main method to look for the /install and /uninstall command-line switches, and to act appropriately.


"Appropriately" in this case means instantiating an instance of the TransactedInstaller class, adding our installer to its Installers collection, and setting a few things up. One of these is to tell the installer the path to the assembly we're installing. We can retrieve this using the Location property of the Assembly class. Oddly, the way the installation stuff wants this information is for us to pass it in an array of strings that contain what are essentially command line parameters. My guess is that the installer classes were built to work with InstallUtil in mind.


The other thing we need to do is pass in an empty Hashtable if we're calling Install. The installer will use this to store some things internally. We don't need to worry about that. When calling Uninstall, we just pass null. Other than that, the code for both cases is pretty similar.


Here's the code:


public class MyService : ServiceBase
{
  // Service stuff omitted for brevity

  static void Main( string [] args )
  {
    string opt = null ;
    if ( args.Length > 1)
    {
      opt = args [0];
    }
  
    if (opt != null && opt.ToLower () == "/install")
    {
       TransactedInstaller ti = new TransactedInstaller ();
       MyInstaller mi = new MyInstaller ();
       ti.Installers.Add (mi);
      String path = String.Format ("/ assemblypath ={0}",
         System.Reflection.Assembly.GetExecutingAssembly ().Location);
      String[] cmdline = {path};
       InstallContext ctx = new InstallContext ("", cmdline );
       ti.Context ( ctx );
       ti.Install ( new Hashtable ());
    }
    else if (opt != null && opt.ToLower () == "/uninstall")
    {
       TransactedInstaller ti = new TransactedInstaller ();
       MyInstaller mi = new MyInstaller ();
       ti.Installers.Add (mi);
      String path = String.Format ("/ assemblypath ={0}",
       System.Reflection.Assembly.GetExecutingAssembly ().Location);
      String[] cmdline = {path};
       InstallContext ctx = new InstallContext ("", cmdline );
       ti.Context ( ctx );
       ti.Uninstall ( null );
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值