C# – How to change the name of the Windows service

cwindows-services

I have a Windows Service and want to change the name of it (as it appears in the Services application). But I'm unsure of the correct way to do so. It appears to be the ServiceName property and searching through my solution I found this:

namespace SI.AService.AService
{
    partial class AService
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            // 
            // AService
            // 
            this.ServiceName = "Company.AService.AService";

        }

        #endregion
    }
}

So this seems to be autogenerated code. What's the proper way of changing it? It says "do not modify the contents of this method with the code editor." So where do I modify it then?

Update: I was updating my Windows Service through the build process in Visual Studio, which apparently has no effect on name changes set up in the service installer. I think it runs an uninstall and install command with the InstallUtil. Instead I had to go to the output directory of the build process where 2 files are located. An msi-file and a setup.exe file. The msi installs the service, BUT there is no name changes. However if I run the setup.exe, it does the same but name changes to the service are included. So I guess the projectinstaller/serviceinstaller are included in the setup.exe and not the other.

Lastly, thank you all for your help.

Best Answer

You might have added an installer by right clicking the design view of your Windows service class. If you have done this then you will find a ProjectInstaller class within your WindowsService project.

By selecting this ProjectInstaller class you will find two installers on its design view -> 1.ServiceInstaller1 2.ServiceProcessInstaller1

Right click the ServiceInstaller1 and select properties. In the properties window change the ServiceName to the name you want to give to your service.

Hope this works...