配置管理器返回null而不是字符串值

22

我正在尝试从存储在工作目录中的App.config文件中检索值,但程序运行时返回null。我非常困惑为什么会这样,并且已经多次查看代码以寻找错误。

这是我的App.config文件代码:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="provider" value="System.Data.SqlClient" />
  </appSettings>
  <connectionStrings>
    <add name="connection" connectionString="Data Source=(local)\SQLEXPRESS;Initial Catalog=Autos;Integrated Security=True;Pooling=False" />
  </connectionStrings>
</configuration>

以下是我的 C# 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.Common;

namespace DataProviderFun
{
  class Program
  {
    static void Main(string[] args)
    {
      string p = ConfigurationManager.AppSettings["provider"];
      string c = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;

      ...
当我运行这段代码时,p = null,c = null。 我已引用了System.Configuration.dll。

2
你的 app.config 文件是否与可执行文件一起正确部署了呢?也就是说,在执行目录中是否存在一个 ApplicationName.exe.config 文件? - Quintin Robinson
1
App.Config是使用的模板。真正的配置文件将是“Program.Exe.Config”文件。这应该在编译项目时完成。 - Marvin Smit
2
你是否偶然将配置添加到了 DLL 项目中? - Aliostad
4个回答

26

你确定配置文件放在了运行应用程序的目录下吗?那个目录中是否有一个名为 <app name>.exe.config 的文件呢?

我只是猜测 - 也许你将 App.Config 文件添加到了不同于 exe 程序集项目的项目中...?

顺便说一下,我复制了你的代码和App.Config到一个干净的项目中,这段代码对我起作用了。所以我会把注意力放在配置文件本身而不是代码上。代码没问题...

希望这可以帮助你,

Ran


我将App.config文件放在我的bin\Debug文件夹中,但是这个文件夹以及与该项目相关的其他文件夹都不包含<app>.exe.config文件。这是一个可执行文件,而不是库。我尝试清理和重建项目,但是这个文件仍然没有出现。 - TheBoss
10
不应手动复制App.config文件。它应该在构建时自动复制并重命名。也许您只是向项目中添加了一个名为App.config的文件,而没有添加“应用程序配置”项?您可以尝试重新将其添加到可执行项目中。 - Ran
2
也许你只是将一个名为App.config的文件添加到了你的项目中,而不是添加一个“应用程序配置”项。这是正确的答案。谢谢Ran。 - TheBoss

1
如果您的配置文件在不同的类库中使用,您必须更改名称为“YourClasslibraryDllname.dll.config”,并且您必须更改配置文件的“复制到输出目录”属性。
Ex:
   YourSolution
       ClassLibrary_1
            ClassLibrary_1.dll.config
            ApplicationConfigurationReader.cs
            ConfigurationConst.cs
       ClassLibrary_2
       ConsoleApp
  1. 将您的配置文件重命名为YourClasslibraryDllname.dll.config
  2. 打开属性窗口
  3. Do Not Copy更改为Copy Always

enter image description here

  1. 添加引用 -> 程序集 -> System.Configuration

  2. 在 ClassLibrary_1 项目中添加以下类

ConfigurationConst 类 using System.Configuration;

public static class ConfigurationConst
{
   public static KeyValueConfigurationCollection Configs;
}

ApplicationConfigurationReader类 使用System.Configuration;

internal class ApplicationConfigurationReader
    {
        public void Read()
        {
            // read assembly
            var ExecAppPath = this.GetType().Assembly.Location;

            // Get all app settings  in config file
            ConfigurationConst.Configs = ConfigurationManager.OpenExeConfiguration(ExecAppPath).AppSettings.Settings;

        }
    }

使用 ClassLibrary_1 读取配置

static void Main(string[] args)
{
    new ApplicationConfigurationReader().Read();
    var Configval = ConfigurationConst.Configs["provider"].Value;            
    Console.ReadKey();
}

我希望你能得到干净的帮助。

这个解决方案对我很有效。我不需要将配置文件重命名为YourClasslibraryDllname.dll.config,只需使用名称“App.config”即可。 - Amit Sharma

0
如果您的调试文件夹中有 .dll 文件,则将您的配置文件重命名为 yourprojectname.dll.config。这在我的情况下有效。

0
如果所有设置都正确,但仍然获取到 null 值,请检查您的 app.config 文件并将 xml 代码替换为以下内容:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>
</configuration>

现在运行你的代码,你可能会看到正确的值


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接