读取当前配置文件的方法

var config = ConfigurationManager.OpenExeConfiguration(Assembly.GetAssembly(typeof(ProjectInstaller)).Location);
            this.serviceInstaller1.ServiceName = config.AppSettings.Settings["ServiceName"].Value;

我发现在有些情况下读取应用程序的配置文件会出现ConfigurationManager.AppSettings读取不到的情况,而用上面的代码却可以。比如Windows Service 的Installer里面动态从配置文件读取ServiceName的时候。

原文地址:https://www.cnblogs.com/nanfei/p/2760725.html