Click here to Skip to main content
15,917,176 members
Home / Discussions / C#
   

C#

 
Questionhow to add new windows service by using C# code Pin
wasimsharp4-Jun-08 19:36
wasimsharp4-Jun-08 19:36 
AnswerRe: how to add new windows service by using C# code Pin
Parwej Ahamad4-Jun-08 19:58
professionalParwej Ahamad4-Jun-08 19:58 
GeneralRe: how to add new windows service by using C# code Pin
wasimsharp4-Jun-08 20:02
wasimsharp4-Jun-08 20:02 
GeneralRe: how to add new windows service by using C# code Pin
mav.northwind4-Jun-08 20:16
mav.northwind4-Jun-08 20:16 
AnswerRe: how to add new windows service by using C# code Pin
telha4-Jun-08 20:44
telha4-Jun-08 20:44 
GeneralRe: how to add new windows service by using C# code Pin
wasimsharp4-Jun-08 20:46
wasimsharp4-Jun-08 20:46 
GeneralRe: how to add new windows service by using C# code Pin
wasimsharp4-Jun-08 23:44
wasimsharp4-Jun-08 23:44 
AnswerRe: how to add new windows service by using C# code Pin
telha5-Jun-08 2:06
telha5-Jun-08 2:06 
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 />
    /// <summary><br />
    /// <br />
    /// </summary><br />
    public enum WindowsServiceAccountType<br />
    {<br />
        LocalService,<br />
        NetworkService,<br />
        LocalSystem,<br />
        User<br />
    }<br />
    /// <summary><br />
    /// <br />
    /// </summary><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 />
        /// <summary><br />
        /// <br />
        /// </summary><br />
        /// <param name="windowsServicePath">Path to folder where Windows Service binary is located.</param><br />
        /// <param name="windowsServiceAssemblyName">Windows Service Assembly Name.</param><br />
        /// <param name="wsAccountType">Windows Service Account Type.(not user type)</param><br />
        public WindowsServiceInstallInfo(string windowsServicePath, string windowsServiceAssemblyName, WindowsServiceAccountType wsAccountType)<br />
            : this("", windowsServicePath, windowsServiceAssemblyName, wsAccountType) { }<br />
<br />
        /// <summary><br />
        /// <br />
        /// </summary><br />
        /// <param name="windowsServiceName">Name of the windows service.</param><br />
        /// <param name="windowsServicePath">Path to folder where windows service assembly stored.</param><br />
        /// <param name="windowsServiceAssemblyName">windows service assembly name.</param><br />
        /// <param name="wsAccountType">Windows service account type (not user type)</param><br />
        public WindowsServiceInstallInfo(string windowsServiceName, string windowsServicePath, string windowsServiceAssemblyName, WindowsServiceAccountType wsAccountType)<br />
            : this(windowsServiceName, "", windowsServicePath, windowsServiceAssemblyName, wsAccountType) { }<br />
<br />
        /// <summary><br />
        /// <br />
        /// </summary><br />
        /// <param name="windowsServiceName">Name of windows service.</param><br />
        /// <param name="description">Description of windows service.</param><br />
        /// <param name="windowsServicePath">Path to folder where windows service binary is stored.</param><br />
        /// <param name="windowsServiceAssemblyName">Windows service assembly name.</param><br />
        /// <param name="wsAccountType">Windows service account type (not user type).</param><br />
        public WindowsServiceInstallInfo(string windowsServiceName, string description, string windowsServicePath, string windowsServiceAssemblyName, WindowsServiceAccountType wsAccountType)<br />
            : this(windowsServiceName, description, windowsServicePath, windowsServiceAssemblyName, wsAccountType, "", "") { }<br />
<br />
        /// <summary><br />
        /// <br />
        /// </summary><br />
        /// <param name="windowsServicePath">Path to the folder where windows service binary resides.</param><br />
        /// <param name="windowsServiceAssemblyName">Windows service assembly name.</param><br />
        /// <param name="wsAccountName">User Name of Windows Service when account type is user.</param><br />
        /// <param name="wsAccountPassword">Password of Windows Service when account type is user.</param><br />
        public WindowsServiceInstallInfo(string windowsServicePath, string windowsServiceAssemblyName, string wsAccountUserName, string wsAccountPassword)<br />
            : this("", windowsServicePath, windowsServiceAssemblyName, wsAccountUserName, wsAccountPassword) { }<br />
<br />
        /// <summary><br />
        /// <br />
        /// </summary><br />
        /// <param name="windowsServiceName">Name of the windows service.</param><br />
        /// <param name="windowsServicePath">Path to the folder where windows service binaries reside.</param><br />
        /// <param name="windowsServiceAssemblyName">Windows service assembly name.</param><br />
        /// <param name="wsAccountUserName">User Name of the Windows Service when account type is User.</param><br />
        /// <param name="wsAccountPassword">User Name of the Windows Service when account type is User.</param><br />
        public WindowsServiceInstallInfo(string windowsServiceName, string windowsServicePath, string windowsServiceAssemblyName, string wsAccountUserName, string wsAccountPassword)<br />
            : this(windowsServiceName, "", windowsServicePath, windowsServiceAssemblyName, wsAccountUserName, wsAccountPassword) { }<br />
        /// <summary><br />
        /// <br />
        /// </summary><br />
        /// <param name="windowsServiceName">Name of the Windows Service.</param><br />
        /// <param name="description">Description of the Windows Service.</param><br />
        /// <param name="windowsServicePath">Path to the folder where Windows Service Binaries Reside.</param><br />
        /// <param name="windowsServiceAssemblyName">Windows Service Assembly Name.</param><br />
        /// <param name="wsAccountUserName">User Name of the Windows Service when Account Type is User.</param><br />
        /// <param name="wsAccountPassword">Password of the Windows Service when Account Type is User.</param><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 />
        /// <summary><br />
        /// Gets or sets the windows service name.<br />
        /// </summary><br />
        public System.String WindowsServiceName<br />
        {<br />
            get<br />
            {<br />
                return _windowsServiceName;<br />
            }<br />
            set<br />
            {<br />
                _windowsServiceName = value;<br />
            }<br />
        }<br />
        /// <summary><br />
        /// Gets or sets the windows service description<br />
        /// </summary><br />
        public string WindowsServiceDescription<br />
        {<br />
            get<br />
            {<br />
                return _wsDescription;<br />
            }<br />
            set<br />
            {<br />
                _wsDescription = value;<br />
            }<br />
        }<br />
        /// <summary><br />
        /// Gets the path where the service binaries reside.<br />
        /// </summary><br />
        public string WindowsServicePath<br />
        {<br />
            get<br />
            {<br />
                return _windowsServicePath;<br />
            }<br />
        }<br />
        /// <summary><br />
        /// Gets the windows Service binary file name.<br />
        /// </summary><br />
        public string WindowsServiceAssemblyName<br />
        {<br />
            get<br />
            {<br />
                return _windowsServiceAssemblyName;<br />
            }<br />
        }<br />
        /// <summary><br />
        /// Gets the account type to start the service.<br />
        /// </summary><br />
        public WindowsServiceAccountType wsAccountType<br />
        {<br />
            get<br />
            {<br />
                return _wsAccountType;<br />
            }<br />
        }<br />
        /// <summary><br />
        /// Gets the user name to start service if type is user.<br />
        /// </summary><br />
        public string wsAccountUserName<br />
        {<br />
            get<br />
            {<br />
                return _wsAccountUserName;<br />
            }<br />
        }<br />
        /// <summary><br />
        /// <br />
        /// </summary><br />
        public string wsAccountPassword<br />
        {<br />
            get<br />
            {<br />
                return _wsAccountPassword;<br />
            }<br />
        }<br />
        #endregion<br />
    }<br />
<br />
    //Another Class<br />
    public class WindowsServiceInstallUtil<br />
    {<br />
        /// <summary><br />
        /// Path to folder where the InstallUtil (.Net SDK) file is located.<br />
        /// </summary><br />
        public static string InstallUtilPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();<br />
<br />
        protected WindowsServiceInstallInfo _wsInstallInfo;<br />
<br />
        /// <summary><br />
        /// Constructor of the class.<br />
        /// </summary><br />
        /// <param name="wsInstallInfo">Object containing the information about the service to install.</param><br />
        public WindowsServiceInstallUtil(WindowsServiceInstallInfo wsInstallInfo)<br />
        {<br />
            _wsInstallInfo = wsInstallInfo;<br />
        }<br />
        /// <summary><br />
        /// Runs Install Util with specific parameters.<br />
        /// </summary><br />
        /// <param name="installUtilArguments">Install Arguments to pass to the service.</param><br />
        /// <returns>Status of Installation.</returns><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 />
        /// <summary><br />
        /// Installs Windows Service<br />
        /// </summary><br />
        /// <returns></returns><br />
        public bool Install()<br />
        {<br />
            return Install("");<br />
        }<br />
        /// <summary><br />
        /// Installs windows Service.<br />
        /// </summary><br />
        /// <param name="logFilePath">Log file to store installation information</param><br />
        /// <returns></returns><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 />
        //now time to uninstall the service<br />
        /// <summary><br />
        /// UnInstalls the Service.<br />
        /// </summary><br />
        /// <returns>The UnInstall Status.</returns><br />
        public bool UnInstall()<br />
        {<br />
            return UnInstall("");<br />
        }<br />
<br />
        /// <summary><br />
        /// Uninstalls the Service.<br />
        /// </summary><br />
        /// <param name="logFilePath">Log file to store uninstall information.</param><br />
        /// <returns>Status on Uninstallation.</returns><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

GeneralRe: how to add new windows service by using C# code Pin
wasimsharp5-Jun-08 21:47
wasimsharp5-Jun-08 21:47 
QuestionWhich Event is Fire ! Pin
nomi4-Jun-08 18:39
nomi4-Jun-08 18:39 
AnswerRe: Which Event is Fire ! Pin
Nouman Bhatti4-Jun-08 18:49
Nouman Bhatti4-Jun-08 18:49 
QuestionRe: Which Event is Fire ! Pin
nomi4-Jun-08 19:11
nomi4-Jun-08 19:11 
AnswerRe: Which Event is Fire ! Pin
Guffa4-Jun-08 19:18
Guffa4-Jun-08 19:18 
QuestionRe: Which Event is Fire ! Pin
nomi4-Jun-08 19:21
nomi4-Jun-08 19:21 
AnswerRe: Which Event is Fire ! Pin
Nouman Bhatti4-Jun-08 19:59
Nouman Bhatti4-Jun-08 19:59 
GeneralRe: Which Event is Fire ! Pin
nomi4-Jun-08 20:07
nomi4-Jun-08 20:07 
GeneralRe: Which Event is Fire ! Pin
Nouman Bhatti4-Jun-08 20:21
Nouman Bhatti4-Jun-08 20:21 
GeneralRe: Which Event is Fire ! Pin
nomi4-Jun-08 20:24
nomi4-Jun-08 20:24 
RantRe: Which Event is Fire ! Pin
Nouman Bhatti4-Jun-08 20:56
Nouman Bhatti4-Jun-08 20:56 
Generalyar there is only one instance for image update nothing Except this ! Pin
nomi4-Jun-08 21:13
nomi4-Jun-08 21:13 
AnswerRe: Which Event is Fire ! Pin
wasimsharp4-Jun-08 21:27
wasimsharp4-Jun-08 21:27 
AnswerRe: Which Event is Fire ! Pin
telha5-Jun-08 2:23
telha5-Jun-08 2:23 
QuestionInternet download buffer problem Pin
MAW304-Jun-08 16:01
MAW304-Jun-08 16:01 
QuestionProjects Don't work on Computers without VB Pin
C# Beginner Nick4-Jun-08 14:37
C# Beginner Nick4-Jun-08 14:37 
AnswerRe: Projects Don't work on Computers without VB Pin
Christian Graus4-Jun-08 17:22
protectorChristian Graus4-Jun-08 17:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.