【问题标题】:ConfigurationManager return null instead of string valuesConfigurationManager 返回 null 而不是字符串值
【发布时间】:2011-05-18 19:48:57
【问题描述】:

我正在尝试从存储在我的工作目录中的 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。

【问题讨论】:

  • 您的 app.config 是否与您的可执行文件一起正确部署?.. IE:在执行目录中是否存在 ApplicationName.exe.config 文件?
  • App.Config 是使用的模板。真正的配置文件将是“Program.Exe.Config”文件。这应该在编译项目时完成。
  • 您有没有将配置添加到 DLL 项目中?

标签: c# .net app-config configurationmanager


【解决方案1】:

您是否确保将配置文件正确放置在您运行应用程序的目录中?该目录中实际上是否有一个名为 .exe.config 的文件?

我只是在这里猜测 - 也许您在另一个项目中添加了 App.Config 文件,然后是您的 exe 程序集项目...?

顺便说一句,我将您的代码和 App.Config 原样复制到了一个干净的项目中,这段代码对我有用。所以我会看配置文件本身的方向,而不是代码。代码很好...

希望这会有所帮助,

跑

【讨论】:

  • 我将 App.config 文件放在我的 bin\Debug 文件夹中,但是此文件夹以及与此项目关联的其他文件夹都不包含 .exe.config 文件。这是一个可执行文件,而不是一个库。我已经尝试清理和重建项目,但没有出现此文件。
  • 您不应手动复制 App.config 文件。它应该在构建时自动复制和重命名。也许您只是在项目中添加了一个名为 App.config 的文件,而不是添加“应用程序配置”项?您可以尝试将其重新添加到可执行项目中。
  • “也许您只是在项目中添加了一个名为 App.config 的文件,而不是添加“应用程序配置”项” 是正确答案。谢谢冉。
【解决方案2】:

如果您的配置文件在不同的类库中使用,您必须更改您的名称YourClasslibraryDllname.dll.config并且您必须更改配置文件复制到输出目录 属性

Ex:
   YourSolution
       ClassLibrary_1
            ClassLibrary_1.dll.config
            ApplicationConfigurationReader.cs
            ConfigurationConst.cs
       ClassLibrary_2
       ConsoleApp
  1. 像这样重命名您的配置文件 YourClasslibraryDllname.dll.config
  2. 打开属性窗口
  3. 将请勿复制更改为始终复制

  1. 添加参考 -> 程序集 -> System.Configuration

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

ConfigurationConst 类使用 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”。
【解决方案3】:

如果所有设置都是正确的,但如果你得到空值,请检查你的 app.config 文件并替换下面的 xml 代码,

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>
</configuration>

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

【讨论】:

    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 1970-01-01
      相关资源
      最近更新 更多