Well I also looked a sample on Code Project for this which calls the InstallUtil utility through code, I guess if you search this site for Dynamic Windows Service Installer, you will come across that....
anyway the code is as:
<br />
using System;<br />
using System.Diagnostics;<br />
using System.Net;<br />
using System.IO;<br />
<br />
namespace ServiceInstaller<br />
{<br />
public enum WindowsServiceAccountType<br />
{<br />
LocalService,<br />
NetworkService,<br />
LocalSystem,<br />
User<br />
}<br />
public class WindowsServiceInstallInfo<br />
{<br />
#region "FIELDS"<br />
<br />
private System.String _windowsServiceName;<br />
private System.String _wsDescription;<br />
private readonly string _windowsServicePath;<br />
private readonly string _windowsServiceAssemblyName;<br />
private WindowsServiceAccountType _wsAccountType;<br />
private readonly string _wsAccountUserName;<br />
private readonly string _wsAccountPassword;<br />
<br />
#endregion<br />
<br />
#region "CONSTRUCTOR"<br />
public WindowsServiceInstallInfo(string windowsServicePath, string windowsServiceAssemblyName, WindowsServiceAccountType wsAccountType)<br />
: this("", windowsServicePath, windowsServiceAssemblyName, wsAccountType) { }<br />
<br />
public WindowsServiceInstallInfo(string windowsServiceName, string windowsServicePath, string windowsServiceAssemblyName, WindowsServiceAccountType wsAccountType)<br />
: this(windowsServiceName, "", windowsServicePath, windowsServiceAssemblyName, wsAccountType) { }<br />
<br />
public WindowsServiceInstallInfo(string windowsServiceName, string description, string windowsServicePath, string windowsServiceAssemblyName, WindowsServiceAccountType wsAccountType)<br />
: this(windowsServiceName, description, windowsServicePath, windowsServiceAssemblyName, wsAccountType, "", "") { }<br />
<br />
public WindowsServiceInstallInfo(string windowsServicePath, string windowsServiceAssemblyName, string wsAccountUserName, string wsAccountPassword)<br />
: this("", windowsServicePath, windowsServiceAssemblyName, wsAccountUserName, wsAccountPassword) { }<br />
<br />
public WindowsServiceInstallInfo(string windowsServiceName, string windowsServicePath, string windowsServiceAssemblyName, string wsAccountUserName, string wsAccountPassword)<br />
: this(windowsServiceName, "", windowsServicePath, windowsServiceAssemblyName, wsAccountUserName, wsAccountPassword) { }<br />
public WindowsServiceInstallInfo(string windowsServiceName, string description, string windowsServicePath, string windowsServiceAssemblyName, string wsAccountUserName, string wsAccountPassword)<br />
: this(windowsServiceName, description, windowsServicePath, windowsServiceAssemblyName, WindowsServiceAccountType.User, wsAccountUserName, wsAccountPassword) { }<br />
<br />
private WindowsServiceInstallInfo(string windowsServiceName, string description, string windowsServicePath, string windowsServiceAssemblyName, WindowsServiceAccountType wsAccountType, string wsAccountUserName, string wsAccountPassword)<br />
{<br />
_windowsServiceName = windowsServiceName;<br />
_wsDescription = description;<br />
_windowsServicePath = windowsServicePath;<br />
_windowsServiceAssemblyName = windowsServiceAssemblyName;<br />
_wsAccountType = wsAccountType;<br />
_wsAccountUserName = wsAccountUserName;<br />
_wsAccountPassword = wsAccountPassword;<br />
<br />
if (_wsAccountType == WindowsServiceAccountType.User && _wsAccountUserName == "")<br />
{<br />
throw new Exception("Username must be provided if AccountType to start the Windows Service is USER");<br />
}<br />
}<br />
#endregion<br />
<br />
#region "PROPERTIES"<br />
public System.String WindowsServiceName<br />
{<br />
get<br />
{<br />
return _windowsServiceName;<br />
}<br />
set<br />
{<br />
_windowsServiceName = value;<br />
}<br />
}<br />
public string WindowsServiceDescription<br />
{<br />
get<br />
{<br />
return _wsDescription;<br />
}<br />
set<br />
{<br />
_wsDescription = value;<br />
}<br />
}<br />
public string WindowsServicePath<br />
{<br />
get<br />
{<br />
return _windowsServicePath;<br />
}<br />
}<br />
public string WindowsServiceAssemblyName<br />
{<br />
get<br />
{<br />
return _windowsServiceAssemblyName;<br />
}<br />
}<br />
public WindowsServiceAccountType wsAccountType<br />
{<br />
get<br />
{<br />
return _wsAccountType;<br />
}<br />
}<br />
public string wsAccountUserName<br />
{<br />
get<br />
{<br />
return _wsAccountUserName;<br />
}<br />
}<br />
public string wsAccountPassword<br />
{<br />
get<br />
{<br />
return _wsAccountPassword;<br />
}<br />
}<br />
#endregion<br />
}<br />
<br />
public class WindowsServiceInstallUtil<br />
{<br />
public static string InstallUtilPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();<br />
<br />
protected WindowsServiceInstallInfo _wsInstallInfo;<br />
<br />
public WindowsServiceInstallUtil(WindowsServiceInstallInfo wsInstallInfo)<br />
{<br />
_wsInstallInfo = wsInstallInfo;<br />
}<br />
private static bool CallInstallUtil(string installUtilArguments)<br />
{<br />
Process proc = new Process();<br />
proc.StartInfo.FileName = Path.Combine(InstallUtilPath, "installutil.exe");<br />
proc.StartInfo.Arguments = installUtilArguments;<br />
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;<br />
proc.StartInfo.RedirectStandardOutput = true;<br />
proc.StartInfo.UseShellExecute = false;<br />
<br />
proc.Start();<br />
<br />
string outputResult = proc.StandardOutput.ReadToEnd();<br />
proc.WaitForExit();<br />
if (proc.ExitCode != 0)<br />
{<br />
return false;<br />
}<br />
return true;<br />
}<br />
public bool Install()<br />
{<br />
return Install("");<br />
}<br />
public virtual bool Install(string logFilePath)<br />
{<br />
string installUtilArguments = GenerateInstallUtilInstallArgs(logFilePath);<br />
return CallInstallUtil(installUtilArguments);<br />
}<br />
<br />
protected string GenerateInstallUtilInstallArgs(string logFilePath)<br />
{<br />
string installUtilArguments = " /account=\"" + _wsInstallInfo.wsAccountType + "\"";<br />
if (_wsInstallInfo.WindowsServiceName != "")<br />
{<br />
installUtilArguments += " /name=\"" + _wsInstallInfo.WindowsServiceName + "\"";<br />
}<br />
if (_wsInstallInfo.WindowsServiceDescription != "")<br />
{<br />
installUtilArguments += " /desc=\"" + _wsInstallInfo.WindowsServiceDescription + "\"";<br />
}<br />
if (_wsInstallInfo.wsAccountType == WindowsServiceAccountType.User)<br />
{<br />
installUtilArguments += " /user=\"" + _wsInstallInfo.wsAccountUserName + "\" /password=\"" + _wsInstallInfo.wsAccountPassword + "\"";<br />
}<br />
<br />
installUtilArguments += " \"" + Path.Combine(_wsInstallInfo.WindowsServicePath, _wsInstallInfo.WindowsServiceAssemblyName) + "\"";<br />
<br />
if (logFilePath.Trim() != "")<br />
{<br />
installUtilArguments += " /LogFile=\"" + logFilePath + "\"";<br />
}<br />
<br />
return installUtilArguments;<br />
}<br />
<br />
public bool UnInstall()<br />
{<br />
return UnInstall("");<br />
}<br />
<br />
public virtual bool UnInstall(string logFilePath)<br />
{<br />
string installUtilArguments = GenerateIntallUtilUnInstallArgs(logFilePath);<br />
return CallInstallUtil(installUtilArguments);<br />
}<br />
<br />
protected string GenerateIntallUtilUnInstallArgs(string logFilePath)<br />
{<br />
string installUtilArgs = " /u ";<br />
if (_wsInstallInfo.WindowsServiceName != "")<br />
{<br />
installUtilArgs += " /name=\"" + _wsInstallInfo.WindowsServiceName + "\"";<br />
}<br />
installUtilArgs += " \"" + Path.Combine(_wsInstallInfo.WindowsServicePath, _wsInstallInfo.WindowsServiceAssemblyName)+"\"";<br />
if (logFilePath.Trim() != "")<br />
{<br />
installUtilArgs += " /LogFile=\"" + logFilePath + "\"";<br />
}<br />
return installUtilArgs;<br />
}<br />
}<br />
}<br />
let me know if you need further assisstance.
Muhammad Talha
|