|
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Collections.Specialized;
using System.ServiceProcess;
using Microsoft.Win32;
namespace MonitoredService
{
/// <summary>
/// Installs a single service "n" number of times. Applies a unique name to the servce using the /name switch
/// </summary>
[RunInstaller(true)]
public class MultipleInstaller : Installer
{
private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller processInstaller;
public MultipleInstaller()
{
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();
processInstaller.Account = System.ServiceProcess.ServiceAccount.User;
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServiceName = "MonitoredServiceExample";
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
/// <summary>
/// Overriden to set the name, user name and password for the service.
/// </summary>
/// <param name="savedState"></param>
protected override void OnBeforeInstall(IDictionary savedState)
{
base.OnBeforeInstall(savedState);
string
name = Context.Parameters["name"],
user = Context.Parameters["user"],
password = Context.Parameters["password"];
if (name != null)
serviceInstaller.ServiceName = name;
if(user != null)
processInstaller.Username = user;
if(password != null)
processInstaller.Password = password;
}
public override void Install(IDictionary stateServer)
{
RegistryKey
system,
currentControlSet,
services,
service,
config;
base.Install(stateServer);
system = Registry.LocalMachine.OpenSubKey("System");
currentControlSet = system.OpenSubKey("CurrentControlSet");
services = currentControlSet.OpenSubKey("Services");
service = services.OpenSubKey(this.serviceInstaller.ServiceName,true);
service.SetValue("Description", "Example MonitoredService implementation");
Console.WriteLine("ImagePath: " + service.GetValue("ImagePath"));
string imagePath = (string)service.GetValue("ImagePath");
imagePath += " -s" + this.serviceInstaller.ServiceName;
service.SetValue("ImagePath", imagePath);
config = service.CreateSubKey("Parameters");
config.Close();
service.Close();
services.Close();
currentControlSet.Close();
}
protected override void OnBeforeUninstall(IDictionary savedState)
{
base.OnBeforeUninstall(savedState);
string name = Context.Parameters["name"];
if (name != null)
serviceInstaller.ServiceName = name;
}
public override void Uninstall(IDictionary stateServer)
{
RegistryKey
system,
currentControlSet,
services,
service;
base.Uninstall(stateServer);
system = Registry.LocalMachine.OpenSubKey("System");
currentControlSet = system.OpenSubKey("CurrentControlSet");
services = currentControlSet.OpenSubKey("Services");
service = services.OpenSubKey(this.serviceInstaller.ServiceName,true);
service.DeleteSubKeyTree("Parameters");
service.Close();
services.Close();
currentControlSet.Close();
}
}//end class
}//end namespace declaration
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Giora Tamir has been Architecting, Designing and Developing software and hardware solutions for over 15 years. As an IEEE Senior member and a talented developer, Giora blends software development, knowledge of protocols, extensive understanding of hardware and profound knowledge of both Unix and Windows based systems to provide a complete solution for both defense and commercial applications. Giora, also known as G.T., now holds the position of Principal Engineer for ProfitLine, Inc. architecting the next generation of .NET applications based on a Service-Oriented-Architecture.
Gioras areas of interest include distributed applications, networking and cryptography in addition to Unix internals and embedded programming.
Founded in 1992, ProfitLine manages hundreds of millions of dollars in annual telecom spend for its prestigious Fortune 1000 client base, such as Merrill Lynch, Charming Shoppes, Macromedia, CNA Financial Corporation, and Constellation Energy Group. ProfitLine's outsourced solution streamlines telecom administrative functions by combining a best practices approach with intelligent technology. For more information about ProfitLine, call 858.452.6800 or e-mail <a href=mailto:sales@profitline.com>sales@profitline.com.