Dynamically specify Windows service name _c# Tutorial during installation of a program developed by C #

Source: Internet
Author: User
This can be difficult for me, is it to be in the development of the code one by one to set the desired name, and then recompile, and then registered as a service?
But what if you change the name in the future? Reset, compile, and register again? This operation is too troublesome!
So I wondered if it would be possible to modify the XML file before each installation by configuring it at installation time, such as adding an XML file to record the service name of the service to be installed.
Operation:
1, first add a configuration file to the service main program root directory, named "Servicesetting.xml":
Copy Code code 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, then add a class file to the service main program root directory, named "SettingHelper.cs":
Copy Code code as follows:

Settinghelper
#region File Description
//-------------------------------------------------------------------------------------------------
Description: Service installation Configuration Help class
Author: Bao Hao sheng
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 Constructors
<summary>
Initialize Service configuration Help class
</summary>
Public Settinghelper ()
{
Initsettings ();
}
#endregion
#region Properties
<summary>
The system is used to flag the name of this service
</summary>
public string ServiceName
{
get {return _servicename;}
}
<summary>
Friendly name for the user to flag the service
</summary>
public string DisplayName
{
get {return _displayname;}
}
<summary>
Description of the service
</summary>
public string Description
{
get {return _description;}
}
#endregion
#region Private Method
#region Initialize Service configuration information
<summary>
Initializing 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 ("Could not find the service name Profile servicesetting.xml! ");
}
}
#endregion
#endregion
#region IDisposable Members
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, modify the constructor public ProjectInstaller () as follows:
Copy Code code 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, the implementation of the installation command:
In the Start menu, locate the Microsoft Visual Studio 2008--> Visual Studio Tools--> Visual Studio 2008 Command prompt, and right-click "Run as Administrator".
At the command line, enter the following command:
Copy Code code as follows:

Setting environment for using Microsoft Visual Studio 2008 x86 tools.
C:\windows\system32>installutil/logfile D:\wsinstalltest.exe

5, when the following text indicates that the installation was successful
Copy Code code as follows:

Installation Success Prompt Information
Microsoft (R). NET Framework Installation Utility version 2.0.50727.5420
Copyright (C) Microsoft Corporation. All rights reserved.
The transaction installation is running.
The installation phase of the installation is starting.
View the contents of the log file to get the progress of the D:\wsinstalltest.exe assembly.
The file is located in the.
Installing assembly "D:\wsinstalltest.exe".
The parameters affected are:
LogtoConsole =
AssemblyPath = D:\wsinstalltest.exe
LogFile =
Installing Service Testme ...
The service Testme has been successfully installed.
Creating EventLog source Testme in log application ...
The setup phase completed successfully and the submit phase is starting.
View the contents of the log file to get the progress of the D:\wsinstalltest.exe assembly.
The file is located in the.
Submitting assembly "D:\wsinstalltest.exe".
The parameters affected are:
LogtoConsole =
AssemblyPath = D:\wsinstalltest.exe
LogFile =
The commit phase has completed successfully.
The transaction installation has been completed.
C:\windows\system32>

You can go to the Services program to see that the service you just installed is already installed.
6. Remark:
Run "SC start testme" start service;
Run "SC stop testme" stop service;
Run the sc delete testme Delete 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.