To install a C# Windows Service without running the ProjectInstaller class in the C# service, use sc.exe.
For example
sc create MyService start= auto binPath= "C:\Program Files (x86)\MyCustomer\MyService\MyService.exe" DisplayName= MyService
Remember, a space is required between the equal sign and the value for the optional parameters.
See http://support.microsoft.com/kb/251192/en-us
You can delete a service with sc.exe.
Before you use sc.exe to delete a service, make sure that all instances of the mmc plug-in “services.msc” are closed and there are no running instances of the service.
Use Process Explorer.exe (http://technet.microsoft.com/en-us/sysinternals/bb795533.aspx) to kill all running instances of the service.
If you don’t, sc delete will give a “Service is marked for deletion” error. This error disappears by rebooting the server or closing the services.msc and closing all running instances of the service.
For example:
sc delete MyService
By default Windows 2000 Professional does not have the sc.exe, you can download it from http://www.dynawell.com/download/reskit/microsoft/win2000/sc.zip
If you want to execute sc.exe from C# code, you could use a function like:
/// <summary> /// Create a Windows service when it does not exist, else configure it. /// </summary> /// <param name="name"></param> /// <param name="displayName"></param> /// <param name="binPath"></param> /// <param name="userName"></param> /// <param name="unecryptedPassword"></param> /// <param name="startupType"></param> public void Configure(string name, string displayName, string binPath, string userName, string unecryptedPassword, string startupType) { // Determine statuptype string startupTypeConverted = string.Empty; switch (startupType) { case "Automatic": startupTypeConverted = "auto"; break; case "Disabled": startupTypeConverted = "disabled"; break; case "Manual": startupTypeConverted = "demand"; break; default: startupTypeConverted = "auto"; break; } // Determine if service has to be created (Create) or edited (Config) StringBuilder builder = new StringBuilder(); if (this.Exists(name)) { builder.AppendFormat("{0} {1} ", "Config", name); } else { builder.AppendFormat("{0} {1} ", "Create", name); } builder.AppendFormat("binPath= \"{0}\" ", binPath); builder.AppendFormat("displayName= \"{0}\" ", displayName); // Only add "obj" when username is not empty. If omitted the "Local System" account will be used if (!string.IsNullOrEmpty(userName)) { builder.AppendFormat("obj= \"{0}\" ", userName); } // Only add password when unecryptedPassword it is not empty and user name is not "NT AUTHORITY\Local Service" or NT AUTHORITY\NetworkService if (!string.IsNullOrEmpty(unecryptedPassword) && !unecryptedPassword.Equals(@"NT AUTHORITY\Local Service") && !unecryptedPassword.Equals(@"NT AUTHORITY\NetworkService")) { builder.AppendFormat("password= \"{0}\" ", unecryptedPassword); } builder.AppendFormat("start= \"{0}\" ", startupTypeConverted); // Execute sc.exe commando using (Process process = new Process()) { process.StartInfo.FileName = @"sc.exe"; process.StartInfo.Arguments = builder.ToString(); process.StartInfo.CreateNoWindow = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.Start(); } }
// Only add password when unecryptedPassword it is not empty and user name is not “NT AUTHORITY\Local Service” or NT AUTHORITY\NetworkService
if (!string.IsNullOrEmpty(unecryptedPassword) && !unecryptedPassword.Equals(@”NT AUTHORITY\Local Service”) && !unecryptedPassword.Equals(@”NT AUTHORITY\NetworkService”))
please fix error in it like this;
if (!string.IsNullOrEmpty(unecryptedPassword) && !userName.Equals(@”NT AUTHORITY\LocalService”) && !userName.Equals(@”NT AUTHORITY\NetworkService”))