C # code installs the Windows service
Using System;
Using System.Collections.Generic;
Using System.ServiceProcess;
Using System.Configuration.Install;
Static Class Program
{
<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main (string[] args)
{
Running the service
if (args. Length = = 0)
{
Servicebase[] ServicesToRun;
ServicesToRun = new servicebase[] {new MyService1 ()};
Servicebase.run (ServicesToRun);
}
Installation Services
else if (args[0]. ToLower () = = "/I" | | Args[0]. ToLower () = = "-I")
{
Try
{
String[] CmdLine = {};
String servicefilename = System.Reflection.Assembly.GetExecutingAssembly (). Location;
Transactedinstaller Transactedinstaller = new Transactedinstaller ();
Assemblyinstaller Assemblyinstaller = new Assemblyinstaller (Servicefilename, cmdline);
TRANSACTEDINSTALLER.INSTALLERS.ADD (Assemblyinstaller);
Transactedinstaller.install (New System.Collections.Hashtable ());
}
catch (Exception ex)
{
String msg = ex. Message;
}
}
Delete Service
else if (args[0]. ToLower () = = "U" | | Args[0]. ToLower () = = "-U")
{
Try
{
String[] CmdLine = {};
String servicefilename = System.Reflection.Assembly.GetExecutingAssembly (). Location;
Transactedinstaller Transactedinstaller = new Transactedinstaller ();
Assemblyinstaller Assemblyinstaller = new Assemblyinstaller (Servicefilename, cmdline);
TRANSACTEDINSTALLER.INSTALLERS.ADD (Assemblyinstaller);
Transactedinstaller.uninstall (NULL);
}
catch (Exception ex)
{
String msg = ex. Message;
}
}
}
}