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

本文介绍了在Windows系统中为服务添加安装器类的方法。需添加继承自System.Configuration.Install.Installer的类,设置服务进程和服务本身的安装器属性。还给出了安装和卸载服务的代码示例,通过命令行开关调用相应操作。
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 );
    }
  }
}
【源码免费下载链接】:https://renmaiwang.cn/s/osp74 InstallUtil.NET Framework提供的一款命令行工具,主要用于安装和卸载Windows服务。它是一个轻量级的实用程序,可以帮助开发者方便地将自定义的服务应用程序部署到Windows操作系统中。在标题和描述中提到的是.NET 4.0版本的InstallUtil,这意味着它兼容.NET Framework 4.0及其更高版本的应用。**服务安装工具InstallUtil的工作原理:**InstallUtil通过调用System.Configuration.Install.Installer类的Install方法来执行服务安装。这个类是.NET框架的一部分,提供了安装和卸载组件的功能。开发者可以继承Installer类并覆盖其方法,以实现自定义的服务安装逻辑。**如何使用InstallUtil:**1. **创建服务项目**:你需要创建一个.NET Framework服务应用程序,包含一个继承自System.ServiceProcess.ServiceBase的类,并实现服务的生命周期方法,如OnStart、OnStop等。2. **配置安装程序类**:创建一个安装程序类,继承自System.Configuration.Install.Installer,并在其中添加ServiceProcessInstaller服务安装器(ServiceInstaller)对象。这两个对象分别用于设置服务的属性(如账户、启动类型等)和服务的显示信息(如服务名、描述等)。3. **编译项目**:编译服务项目,生成一个可执行文件(通常是.exe文件)。4. **运行InstallUtil**:打开命令行,找到.NET Framework的安装目录下的`%windir%\Microsoft.NET\
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值