[Repost] Write automatic installed Windows services (no installation command installutil)

xiaoxiao2021-03-06  20

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 .

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 !................

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 limited the idea of ​​my service being self-installation, 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 ARRAY OF STRINGS That Contain What Are Essential 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 do not 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 ("/ askMBLYPATH = {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 ("/ askMBLYPATH = {0}",

System.Reflection.Assembly.GetexecutingAssembly () .location;

String [] cmdline = {path};

InstallContext CTX = New InstallContext ("", CMDLINE);

Ti.Context (CTX);

Ti.unInstall (NULL);

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-43734.html

New Post(0)