【参考资料】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 "";
}
} }

C#实现windows服务安装,服务名可配置时出问题(无法创建 ProjectInstaller 安装程序类型的实例)的更多相关文章

  1. 解决:Windows安装Composer及全局配置时提示部分.dll结尾的php扩展文件找不到指定的模板

    当安装Composer或者全局配置时出现.dll扩展文件找不到指定模板,如下图: 解决办法: 打开php.ini,将extension_dir 改为绝对路径即可 例如:

  2. CentOS7安装CDH 第四章:CDH的版本选择和安装方式

    相关文章链接 CentOS7安装CDH 第一章:CentOS7系统安装 CentOS7安装CDH 第二章:CentOS7各个软件安装和启动 CentOS7安装CDH 第三章:CDH中的问题和解决方法 ...

  3. Ubuntu下软件安装方式、PATH配置、查找安装位置

    Ubuntu 18.04, 安装方式 目前孤知道的Ubuntu下安装软件方式有3种(命令): 1.make 2.apt/apt-get 3.dpkg 方式1基于软件源码安装,需要经历配置(可选).编译 ...

  4. MAVEN(一) 安装和环境变量配置

    一.安装步骤 1.安装maven之前先安装jdk,并配置好环境变量.确保已安装JDK,并 “JAVA_HOME” 变量已加入到 Windows 环境变量. 2.下载maven 进入官方网站下载网址如下 ...

  5. jdk11下载安装及环境变量配置

    jdk11下载安装及环境变量配置 官网地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-50666 ...

  6. Java中jdk安装与环境变量配置

    Java中jdk安装与环境变量配置 提示:下面是jdk1.7和jdk1.8的百度网盘链接 链接:https://pan.baidu.com/s/1SuHf4KlwpiG1zrf1LLAERQ 提取码: ...

  7. Android Studio安装及主题字体配置

    在2013 Google I/O 大会上,谷歌推出了自家全新的安卓软件集成开发工具 Android Studio,这是 Google 基于 IntelliJ IDEA 改动而来. 谷歌称 Androi ...

  8. Vbox安装CentOS7及网络配置

    安装CentOS7及网络配置 Vbox和其他虚拟机一样,安装完成一个虚拟机,需要配置网络才能实现物理主机和虚拟机之间的访问.虚拟主机和Internet(外网)的访问 1.设置Vbox全局网络 单击主界 ...

  9. Ubuntu 安装 SSH server 并配置 SSH 无密码登录

    https://hinine.com/install-and-configure-ssh-server-on-ubuntu/ Ubuntu 安装 SSH server 并配置 SSH 无密码登录 发表 ...

随机推荐

  1. weblogic域备份脚本

    一直一来,由于空间问题,weblogic域很少备份,偶尔会手动备份一次,这运维做得不称职,今天有时间,写个小脚本来定时备份. 1.脚本备份文件目录结构 [weblogic@mylinux ~]$ tr ...

  2. ZooKeeper管理分布式环境中的数据

    Reference: http://www.cnblogs.com/wuxl360/p/5817549.html 本节本来是要介绍ZooKeeper的实现原理,但是ZooKeeper的原理比较复杂,它 ...

  3. C#学习笔记(18)——C#构造函数中this和base的使用

    说明(2017-7-21 10:29:44): 1. 关于构造函数里的this和base,在网上查了很多资料,但都没有讲的很清楚的,或者是能让我看懂的,感觉都是叽叽歪歪,罗里吧嗦,磨磨唧唧的,有的直接 ...

  4. iOS同步后couldn't load project

    使用Cornerstone同步后,有时候出现下面的现象,点击运行,提示“couldn't load project” 解决方法: 1.鼠标右键点击工程“XX_Internal”,选择“show in ...

  5. iOS增加pch预加载文件

    1.)  打开你的Xcode工程. 在Supporting Files目录下,选择 File > New > File > iOS > Other > PCH File ...

  6. Android ListView的使用(一)

    初次接触listview,以为直接在Android Studio 中将控件给拖过去,就能够使用了,结果半天显示不了. 后来总算知道原因了. 先上代码: activity_main.xml 显示页面 里 ...

  7. Aspose Linux下字体找不到报错

    http://www.aspose.com/docs/display/cellsnet/Smart+Markers http://www.aspose.com/docs/display/cellsja ...

  8. LVS的优点和缺点

    LVS的优点是:1.抗负载能力强.是工作在网络4层之上仅作分发之用,没有流量的产生,这个特点也决定了它在负载均衡软件里的性能最强的,对内存和cpu资源消耗比较低.2.配置性比较低,这是一个缺点也是一个 ...

  9. sublime双击选择全选带中划线

    // Characters that are considered to separate words "word_separators": "./\\()\" ...

  10. RavenDb学习(七) 异步工作以及维度查询

    .异步执行 var entity = new Company {Name = "Async Company #2", Id = "companies/2"}; ...