【问题标题】:configuration system failed to initialize, external config file配置系统初始化失败,外部配置文件
【发布时间】:2019-08-12 01:48:37
【问题描述】:

我的 app.config 文件有问题。当配置文件在exe文件旁边时,它可以完美运行。
但是当我将外部配置文件放到另一个网络驱动器时,它会抛出“配置系统初始化失败”错误。
我应该如何从应用配置链接到外部配置文件以使其工作?

代码如下:

Program.cs
static void Main(string[] args)
{
   var conn = ConfigurationManager.ConnectionStrings["conn"];
   Console.WriteLine("ConnStr01={0}", conn);
   Console.Read();
}

App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <connectionStrings configSource="U:\config\conStr.config">
  </connectionStrings>
</configuration>

conStr.config
<?xml version="1.0" encoding="utf-8" ?>
<connectionStrings>
  <add name="conn"
    connectionString="Data Source={server};Initial Catalog={database};Persist Security Info=True;User ID={username};Password={password}"
    providerName="System.Data.SqlClient"/>
</connectionStrings>

【问题讨论】:

  • 问题:您是否尝试过在App.config 中使用file="path\to\file" 而不是configSource="\path\to\file"
  • 我试过这个,但它会抛出一个“无法识别的属性'文件'”错误

标签: c# .net config


【解决方案1】:
  1. 不要使用文件属性,使用 configSource - 因为文件仅适用于 appSettings。同样,对于 appSettings:ConfigSource 完全替代(不合并)
  2. configSource 必须引用与配置文件位于同一目录或子目录中的文件。

ASP.NET web.config: configSource vs. file attributes

【讨论】:

    猜你喜欢
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 2014-09-28
    • 1970-01-01
    相关资源
    最近更新 更多