C# ConfigurationManager.AppSettings在项目安装程序中获取错误
我的项目安装程序代码如上所述,它不会得到任何编译错误,但当我尝试安装它时,它将回滚,因为它将抛出异常System.Null referenceException。我还要再次确认我的AppConfig没有任何问题C# ConfigurationManager.AppSettings在项目安装程序中获取错误,c#,configuration,configurationmanager,C#,Configuration,Configurationmanager,我的项目安装程序代码如上所述,它不会得到任何编译错误,但当我尝试安装它时,它将回滚,因为它将抛出异常System.Null referenceException。我还要再次确认我的AppConfig没有任何问题 [RunInstaller(true)] public partial class ProjectInstaller : Installer { private ServiceInstaller m_serviceInstaller;
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
private ServiceInstaller m_serviceInstaller;
private ServiceProcessInstaller m_processInstaller;
public ProjectInstaller()
{
string Names= ConfigurationManager.AppSettings["Names"].ToString();
System.Diagnostics.Debugger.Break();
m_serviceInstaller = new System.ServiceProcess.ServiceInstaller();
m_processInstaller = new System.ServiceProcess.ServiceProcessInstaller();
//
// serviceProcessInstaller1
//
m_processInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
m_processInstaller.Password = null;
m_processInstaller.Username = null;
//
// serviceInstaller1
//
m_serviceInstaller.ServiceName = "Testing" + Names;
m_serviceInstaller.DisplayName = "Testing" + Names;
m_serviceInstaller.Description = "Testing" + Names;
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
m_processInstaller,
m_serviceInstaller});
}
}
我可以知道哪里出了问题。也有同样的问题。默认情况下,在服务安装阶段,我们阅读的是\InstallUtil.exe.Config,例如C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe.Config 解决方法是手动加载正确的配置:
<appSettings>
<add key="Names" value="BOC" />
</appSettings>
请检查配置文件是否已在应用程序的运行路径中发布/复制
using System.Reflection;
...
Configuration config = ConfigurationManager.OpenExeConfiguration(
Assembly.GetExecutingAssembly().Location);
KeyValueConfigurationElement element = config.AppSettings.Settings["Names"];
string names = null;
if (element != null)
names = element.Value;