代码之家  ›  专栏  ›  技术社区  ›  Robert MacLean

应用程序目录文件夹外的.NET配置文件configSource

  •  86
  • Robert MacLean  · 技术社区  · 16 年前

    我有两个应用程序,一个是控制台应用程序,另一个是ASP。NET应用程序。它们都需要知道相同的appSettings和connectionStrings。因此,理想情况下,我想使用app.config.web.config文件的configSource属性将其指向一个中心位置。例如

    <connectionStrings configSource="D:\connectionStrings.config"/>
    <appSettings configSource="D:\appSettings.config"/>
    

    然而,这失败了,出现了一个错误:

    configSource属性无效。:configSource“D:\appSettings.config”无效。它必须引用与配置文件位于同一目录或子目录中的文件。

    是否仍然可以使用配置管理器appSettings/connectionString并从外部位置获取值?

    11 回复  |  直到 16 年前
        1
  •  107
  •   Community CDub    7 年前

        2
  •  35
  •   user223066 user223066    15 年前

    在appSettings下,您可以使用file=而不是configSource=

        3
  •  15
  •   Iain M Norman    16 年前

    似乎就是这样。configSource必须位于同一文件夹或更深的文件夹中。

    可以 ,虽然我不确定你 应该 ,使用NTFS硬链接。[疯狂的笑容]

        4
  •  10
  •   Serexx    9 年前

    Visual Studio 2015

    因此,当您在Web中定义了配置部分时。配置如下:

     <section name="mySpecialConfig" type="System.Configuration.AppSettingsSection" requirePermission="false" />
    

      <mySpecialConfig configSource="bin\MySpecialConfig.config">
      </mySpecialConfig>
    

    以便configSource指向物理bin\MySpecialConfig.config文件 不到链接 另外,请注意,路径是 相对的 物理路径。

        5
  •  8
  •   Richard    16 年前

    您可以从任意位置加载配置, 但是 它将无法通过ConfigurationManager的静态属性访问:

    Configuration myConfig = ConfigurationManager.OpenExeConfiguration(path)
    

        6
  •  5
  •   Community CDub    8 年前

    解决方案:在web.config中,使用configSource指向本地配置文件。由于。网络限制,这必须处于或低于根配置文件的级别。我只是指向应用程序文件夹中的一个文件:

    <connectionStrings configSource="ConnectionStrings.config" />
    

    <connectionStrings>
        <clear/>
        <add name="connString1" connectionString="connString1 info goes here"/>
        <add name="connString2" connectionString="connString2 info goes here"/>
    </connectionStrings>  
    

    现在的诀窍。在应用程序文件夹中创建一个指向外部共享配置文件的Windows符号链接。您需要管理员权限才能执行此操作:

    mklink ConnectionStrings.config \\someServer\someShare\someFolder\ConnectionStrings.config
    

    我们刚刚智胜。网。配置系统将使用configSource设置在名为ConnectionStrings.config的本地文件中查找连接字符串。符号链接看起来像一个文件。Net,符号链接解析为共享配置文件。

    警告:对共享文件的更改不会自动触发中的应用程序重新启动。网。对于IIS,需要手动重新启动网站或应用程序池。

    由于需要管理权限来创建符号链接,这种方法可能不适用于所有人。如果共享文件位于同一逻辑驱动器上,则有两种相关的替代方案可能有效——硬盘链接和连接。看 this discussion this discussion 了解更多信息。

        7
  •  3
  •   freggel    16 年前

    您可以在 machine.config

        8
  •  2
  •   Robert MacLean    16 年前

        9
  •  2
  •   Community CDub    8 年前
        10
  •  1
  •   alexs    7 年前

    您可以使用 file 属性而不是 configSource

    here 正在处理

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <appSettings file="..\..\..\..\..\..\ExternalFile.config"></appSettings>
    </configuration>
    

    路径相对于输出目录。

    然后在ExternalFile.config中,您只需添加 appSettings 章节

    <appSettings>
        <add key="SomeKey" value="alue"/>
    </appSettings>
    
        11
  •  0
  •   bobt    3 年前

    1. 如果是Console项目或单元测试项目:

    <connectionStrings configSource="connectionString.config"/>

    1. 如果是Web项目:

    在web配置中执行以下操作: <connectionStrings configSource="bin\connectionString.config"/>

    请注意,如果将connectionString.config文件设置为Copy if Newer,则Visual Studio 2019有时不会检测到更改。因此,您可以将其设置为“始终复制”。

    推荐文章