C # specify the windows Service name dynamically during installation of the developed program

Source: Internet
Author: User

This makes me difficult. Do you need to set the desired name one by one in the development code, re-compile it, and register it as a service?
But what if I want to change the name in the future? Re-set, compile, and register? This operation is too troublesome!
So I thought I could not configure it during installation. For example, add an xml file to record the service name of the service to be installed, and modify the xml file before each installation.
Operation:
1. First, add a configuration file to the root directory of the Service master program and name it "ServiceSetting. xml ": Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Settings>
<ServiceName> testme </ServiceName>
<DisplayName> testmedisplay </DisplayName>
<Description> This is just a test. </Description>
</Settings>

2. Add a class file to the root directory of the Service master program and name it "SettingHelper. cs ":Copy codeThe Code is as follows: SettingHelper
# Region file description
// Configure //-------------------------------------------------------------------------------------------------
// Description: service installation and configuration help class
// Author: BAO haocheng
// Time: 2012-05-10
// Configure //-------------------------------------------------------------------------------------------------
# Endregion
Using System;
Using System. IO;
Using System. Xml;
/// <Summary>
/// Service installation and configuration help class
/// </Summary>
Internal class SettingHelper: IDisposable
{
# Region private member
Private string _ ServiceName;
Private string _ DisplayName;
Private string _ Description;
# Endregion
# Region Constructor
/// <Summary>
/// Initialize the service configuration help class
/// </Summary>
Public SettingHelper ()
{
InitSettings ();
}
# Endregion
# Region attributes
/// <Summary>
/// The system is used to mark the name of this service
/// </Summary>
Public string ServiceName
{
Get {return _ ServiceName ;}
}
/// <Summary>
/// Indicates the friendly name of the Service to the user
/// </Summary>
Public string DisplayName
{
Get {return _ DisplayName ;}
}
/// <Summary>
/// Service description
/// </Summary>
Public string Description
{
Get {return _ Description ;}
}
# Endregion
# Region private Method
# Region initialize service configuration information
/// <Summary>
/// Initialize 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 configuration file ServiceSetting. xml cannot be found! ");
}
}
# Endregion
# Endregion
# Region IDisposable Member
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:Copy codeThe Code is 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, choose Microsoft Visual Studio 2008> Visual Studio Tools> Visual Studio 2008 command prompt, and right-click Run as administrator ".
Enter the following command in the command line:Copy codeThe Code is as follows: Setting environment for using Microsoft Visual Studio 2008x86 tools.
C: \ Windows \ system32> installutil/logfile d: \ wsinstalltest.exe

5. When the following text appears, it indicates that the installation is successful.Copy codeThe Code is as follows:
Microsoft (R). NET Framework installation utility version 2.0.50727.5420
Copyright (C) Microsoft Corporation. All rights reserved.
Installing transaction processing.
Starting the "installation" phase of installation.
View the log file to get the progress of the d: \ wsinstalltest.exe assembly.
The file is located.
Installing assembly "d: \ wsinstalltest.exe ".
The affected parameters are:
Logtoconsole =
Assemblypath = d: \ wsinstalltest.exe
Logfile =
Installing Service testme...
The testme service is successfully installed.
Creating EventLog source testme in log Application...
The "installation" phase has been completed successfully and is in the "Submit" phase.
View the log file to get the progress of the d: \ wsinstalltest.exe assembly.
The file is located.
Submitting assembly "d: \ wsinstalltest.exe ".
The affected parameters are:
Logtoconsole =
Assemblypath = d: \ wsinstalltest.exe
Logfile =
The "Submit" phase has been completed successfully.
Transaction Processing and installation have been completed.
C: \ Windows \ system32>

You can go to the "service" program to check that the service you just installed has been installed.
6. Remarks:
Run SC start testme to start the service;
Run SC stop testme to stop the service;
Run "SC delete testme" to delete the service.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.