简体   繁体   English

如何更改Windows服务的名称?

[英]How do I change the name of my Windows service?

I have a Windows Service and want to change the name of it (as it appears in the Services application). 我有Windows服务,并且想要更改它的名称(如“服务”应用程序中所示)。 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: 它似乎是ServiceName属性,在我的解决方案中进行搜索后发现:

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. 更新:我正在通过Visual Studio中的构建过程更新Windows Service,这显然对在服务安装程序中设置的名称更改没有影响。 I think it runs an uninstall and install command with the InstallUtil. 我认为它通过InstallUtil运行卸载和安装命令。 Instead I had to go to the output directory of the build process where 2 files are located. 相反,我不得不转到构建过程的输出目录,在该目录中有2个文件。 An msi-file and a setup.exe file. 一个msi文件和一个setup.exe文件。 The msi installs the service, BUT there is no name changes. msi安装服务,但没有名称更改。 However if I run the setup.exe, it does the same but name changes to the service are included. 但是,如果我运行setup.exe,它会执行相同的操作,但是会包括对该服务的名称更改。 So I guess the projectinstaller/serviceinstaller are included in the setup.exe and not the other. 因此,我猜想projectinstaller / serviceinstaller包含在setup.exe中,而不是其他。

Lastly, thank you all for your help. 最后,感谢大家的帮助。

You might have added an installer by right clicking the design view of your Windows service class . 您可能通过右键单击Windows服务类设计视图来添加安装程序。 If you have done this then you will find a ProjectInstaller class within your WindowsService project. 如果执行了此操作,则将在WindowsService项目中找到一个ProjectInstaller类

By selecting this ProjectInstaller class you will find two installers on its design view -> 1.ServiceInstaller1 2.ServiceProcessInstaller1 通过选择此ProjectInstaller类,您将在其设计视图上找到两个安装程序-> 1.ServiceInstaller1 2.ServiceProcessInstaller1

Right click the ServiceInstaller1 and select properties . 右键单击ServiceInstaller1,然后选择属性 In the properties window change the ServiceName to the name you want to give to your service. 在属性窗口中, 将ServiceName更改为要提供给服务的名称。

Hope this works... 希望这可以...

You have to add an installer class to your Windows service project. 您必须将安装程序类添加到Windows服务项目中。 Then in its designer you add ServiceInstaller and ServiceProcessInstaller, in objects of these two classes you can specify Service name, display name for service, user etc. 然后在其设计器中添加ServiceInstaller和ServiceProcessInstaller,在这两个类的对象中,您可以指定服务名称,服务的显示名称,用户等。

You can see detailed explanation here: 您可以在此处查看详细说明:
http://utkarshpuranik.wordpress.com/2010/06/07/creating-installing-debugging-windows-services/ http://utkarshpuranik.wordpress.com/2010/06/07/creating-installing-debugging-windows-services/

In Visual Studio 2010, you could double click the entry for the service file in the Solution Explorer (named "AService" in your case). 在Visual Studio 2010中,可以双击解决方案资源管理器中服务文件的条目(在本例中为“ AService”)。 In the properties window, just change the entry under "ServiceName". 在属性窗口中,只需更改“ ServiceName”下的条目。

in VS2012 and 2015, if you want to change it in VS designer, double click on your service class file eg MyCustomService.cs and then in design view right click and choose Properties. 在VS2012和2015中,如果要在VS设计器中进行更改,请双击服务类文件(例如MyCustomService.cs),然后在设计视图中右键单击并选择“属性”。

在此处输入图片说明

that gives you the option to visually change the service name. 这使您可以选择直观地更改服务名称。

在此处输入图片说明

It depends on the way you install it 这取决于安装方式

  • If you use SC.EXE then just change the display name parameter 如果使用SC.EXE,则只需更改显示名称参数
  • If you use ServiceInstaller Class then change its DisplayName 如果使用ServiceInstaller类,则更改其DisplayName

None of the above work for me, so sharing what works actually Go to your file "ProjectInstaller.Designer.cs" and upadate the line: 以上对我来说都不起作用,因此共享实际起作用的内容转到您的文件“ ProjectInstaller.Designer.cs”并更新该行:

this.serviceInstaller1.ServiceName = "Updated Name";

It should be under method: InitializeComponent() 应该在方法下: InitializeComponent()

Hope it helps someone!!! 希望它可以帮助某人!

A picture sometime worth thousand words so i thought i gave you this image with annotation: 一张有时值一千个单词的图片,所以我想我给了这张带注释的图像:

It should be simple. 应该很简单。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM