【参考资料】Windows 安装服务 的两种方式
一、问题出在System.Configuration.ConfigurationManager.AppSettings[""].ToString()无法读取 config文件配置
二、ProjectInstaller.Designer.cs源码
using System.Xml; namespace ServiceHost
{
partial class ProjectInstaller
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region 组件设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller()
{
Description = "Description ",
DisplayName = Get_ConfigValue(this.GetType().Assembly.Location + ".config", "JobDisplayName"),//System.Configuration.ConfigurationManager.AppSettings["JobDisplayName"].ToString(),
ServiceName = Get_ConfigValue(this.GetType().Assembly.Location + ".config", "JobServiceName"),//System.Configuration.ConfigurationManager.AppSettings["JobServiceName"].ToString(),
StartType = System.ServiceProcess.ServiceStartMode.Automatic
};
this.serviceInstaller2 = new System.ServiceProcess.ServiceInstaller()
{
Description = "Description ",
DisplayName = Get_ConfigValue(this.GetType().Assembly.Location + ".config", "InterfaceDisplayName"), //System.Configuration.ConfigurationManager.AppSettings["InterfaceDisplayName"].ToString(),
ServiceName = Get_ConfigValue(this.GetType().Assembly.Location + ".config", "InterfaceServiceName"),//System.Configuration.ConfigurationManager.AppSettings["InterfaceServiceName"].ToString(),
StartType = System.ServiceProcess.ServiceStartMode.Automatic
};
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null; //
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1,
this.serviceInstaller2}); } #endregion private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller2; protected static string Get_ConfigValue(
style="color: #0000ff;">string configpath, string strKeyName)
{
using (XmlTextReader tr = new XmlTextReader(configpath))
{
while (tr.Read())
{
if (tr.NodeType == XmlNodeType.Element)
{
if (tr.Name == "add" && tr.GetAttribute("key") == strKeyName)
{
return tr.GetAttribute("value");
}
}
}
}
return "";
}
} }