The program developed by c dynamically specifies the windows service name when it is installed

  • 2020-05-07 20:15:34
  • OfStack

I was stumped. Did I want to set the desired name one by one in the developed code and then recompile it and register it as a service?
But what if I have to change the name in the future? Reset, compile, register again? This operation is too troublesome!
So I wondered if I could configure the installation, such as adding an xml file to record the service name of the service to be installed, and then modify the xml file before each installation.
Operation:
1. First, add a configuration file to the root directory of the main service program, and name it "ServiceSetting.xml" :
 
<?xml version="1.0" encoding="utf-8" ?> 
<Settings> 
<ServiceName>testme</ServiceName> 
<DisplayName>testmedisplay</DisplayName> 
<Description> This is just a test </Description> 
</Settings> 

2. Then add a class file to the root directory of the main service program, named "SettingHelper.cs" :
 
SettingHelper 
#region  The file  
//------------------------------------------------------------------------------------------------- 
//  Description: service installation configuration help class  
//  Author: bao haosheng  
//  Time: 2012-05-10 
//------------------------------------------------------------------------------------------------- 
#endregion 
using System; 
using System.IO; 
using System.Xml; 
/// <summary> 
///  Service installation configuration help class  
/// </summary> 
internal class SettingHelper : IDisposable 
{ 
#region  Private members  
private string _ServiceName; 
private string _DisplayName; 
private string _Description; 
#endregion 
#region  The constructor  
/// <summary> 
///  Initialize the service configuration help class  
/// </summary> 
public SettingHelper() 
{ 
InitSettings(); 
} 
#endregion 
#region  attribute  
/// <summary> 
///  The system is used to mark the name of this service  
/// </summary> 
public string ServiceName 
{ 
get { return _ServiceName; } 
} 
/// <summary> 
///  Marks the friendly name of the service to the user  
/// </summary> 
public string DisplayName 
{ 
get { return _DisplayName; } 
} 
/// <summary> 
///  Description of service  
/// </summary> 
public string Description 
{ 
get { return _Description; } 
} 
#endregion 
#region  Private methods  
#region  Initialize the service configuration information  
/// <summary> 
///  Initialize the service configuration information  
/// </summary> 
private void InitSettings() 
{ 
string root = System.Reflection.Assembly.GetExecutingAssembly().Location; 
string xmlfile = root.Remove(root.LastIndexOf('\\') + 1) + "ServiceSetting.xml"; 
if (File.Exists(xmlfile)) 
{ 
XmlDocument doc = new XmlDocument(); 
doc.Load(xmlfile); 
XmlNode xn = doc.SelectSingleNode("Settings/ServiceName"); 
_ServiceName = xn.InnerText; 
xn = doc.SelectSingleNode("Settings/DisplayName"); 
_DisplayName = xn.InnerText; 
xn = doc.SelectSingleNode("Settings/Description"); 
_Description = xn.InnerText; 
doc = null; 
} 
else 
{ 
throw new FileNotFoundException(" The service name profile could not be found  ServiceSetting.xml ! "); 
} 
} 
#endregion 
#endregion 
#region IDisposable  Members of the  
private bool disposed = false; 
public void Dispose() 
{ 
Dispose(true); 
GC.SuppressFinalize(this); 
} 
protected virtual void Dispose(bool disposing) 
{ 
if (!this.disposed) 
{ 
if (disposing) 
{ 
//managed dispose 
_ServiceName = null; 
_DisplayName = null; 
_Description = null; 
} 
//unmanaged dispose 
} 
disposed = true; 
} 
~SettingHelper() 
{ 
Dispose(false); 
} 
#endregion 
} 

3. Modify the ProjectInstaller.cs file, and modify the constructor public ProjectInstaller() as follows:
 
ProjectInstaller 
using System.ComponentModel; 
using System.Configuration.Install; 
namespace WSInstallTest 
{ 
[RunInstaller(true)] 
public partial class ProjectInstaller : Installer 
{ 
public ProjectInstaller() 
{ 
InitializeComponent(); 
using (SettingHelper setting = new SettingHelper()) 
{ 
serviceInstaller1.ServiceName = setting.ServiceName; 
serviceInstaller1.DisplayName = setting.DisplayName; 
serviceInstaller1.Description = setting.Description; 
} 
} 
//end of class 
} 
} 

4. Execute the installation command:
In the start menu, find "Microsoft Visual Studio 2008" -- > "Visual Studio Tools" - > "Visual Studio 2008 command prompt", right click "run as administrator".
Enter the following command on the command line:
 
Setting environment for using Microsoft Visual Studio 2008 x86 tools. 
C:\Windows\system32>installutil /logfile d:\wsinstalltest.exe 

5. When the following text appears, the installation is successful
 
 Prompt for successful installation  
Microsoft (R) .NET Framework  Install the utility version  2.0.50727.5420 
 All rights reserved (C) Microsoft Corporation . All rights reserved.  
 Running transaction installation.  
 The "install" phase of the installation is beginning.  
 View the contents of the log file for information  d:\wsinstalltest.exe  Progress of the assembly.  
 The file is located at   .  
 Installing the assembly" d:\wsinstalltest.exe ".  
 The parameter affected is : 
logtoconsole = 
assemblypath = d:\wsinstalltest.exe 
logfile = 
 Installing service  testme... 
 The service was successfully installed  testme .  
 Is the log  Application  Created in the  EventLog  The source  testme... 
 The install phase has completed successfully and the commit phase is beginning.  
 View the contents of the log file for information  d:\wsinstalltest.exe  Progress of the assembly.  
 The file is located at   .  
 Assembly being submitted" d:\wsinstalltest.exe ".  
 The parameter affected is : 
logtoconsole = 
assemblypath = d:\wsinstalltest.exe 
logfile = 
 The "commit" phase has been successfully completed.  
 The transaction installation has completed.  
C:\Windows\system32> 

You can go to the services program to see that the service you just installed is installed.
6. Remarks:
Run sc start testme startup service;
Run "sc stop testme" to stop service;
Run "sc delete testme" to delete the service.

Related articles: