【问题标题】:NULL value in AppSettings - Accessing config file from Class LibraryAppSettings 中的 NULL 值 - 从类库访问配置文件
【发布时间】:2017-04-19 14:33:22
【问题描述】:

我正在尝试从 appSettings 读取值,但得到 null 值作为回报。还在我的类文件中的element 变量中获取null 值。下面是我的类文件内容。

using System;
using System.Configuration;

namespace SampleDLL
{
    public class Class1
    {
        private static string GetAppSetting(Configuration config, string key)
        {
            var testValue = ConfigurationManager.AppSettings[key]; //Alternate option.
            var element = config.AppSettings.Settings[key];
            if (element == null) return string.Empty;
            var value = element.Value;
            return !string.IsNullOrEmpty(value) ? value : string.Empty;
        }

        public void GetDataAppConfig()
        {
            Configuration config = null;
            var exeConfigPath = GetType().Assembly.Location;
            try
            {
                config = ConfigurationManager.OpenExeConfiguration(exeConfigPath);
            }
            catch (Exception exception)
            {
                //handling exception here.
            }
            if (config != null)
            {
                var myValue = GetAppSetting(config, "myKey");
            }
        }
    }
}

我的 app.config 文件如下

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="myKey" value="TestValue"/>
  </appSettings>
</configuration>

System.Configuration 的引用存在于我的类库项目中。我正在从同一解决方案的另一个项目中存在的一个 Windows 窗体中调用 GetDataAppConfig 窗体。

已编辑

我正在使用我的类库项目中的配置文件。

我的 WinForm 类如下,我用它来调用类库项目的 classClass1 中的 GetDataAppConfig

using System;
using System.Windows.Forms;
using SampleDLL;

namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var class1 = new Class1();
            class1.GetDataAppConfig();
        }
    }
}

【问题讨论】:

  • 尝试使用AppSettingsSection 而不是NameValueCollection。喜欢config.GetSection("appSettings").Settings[key].Value;
  • @Liam 我不想使用客户端应用程序中存在的配置文件。
  • 当然,即使您确实做到了这一点,添加到客户端应用程序的任何配置都会覆盖您自己的配置。基本上,不要这样做,你只是让自己头疼,你可以不这样做。

标签: c# visual-studio visual-studio-2012


【解决方案1】:

问题是您使用错误的配置文件调用OpenExeConfiguration(),通过使用确定配置文件

var exeConfigPath = GetType().Assembly.Location;

您实际上是在使用定义 Class1 类型的类库的路径,而您实际上想要加载引用 Client1 的当前客户端的配置(在您的情况下是 WinForm 应用程序)。

试试这个:

config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

MSDN


更新:

由于您想将配置文件嵌入到您的类库项目中,您必须按照以下步骤操作:

  • 将文件名从 App.Config 更改为其他名称(这样它就不会与客户端的配置文件冲突)。

  • 把文件的Build Action改成Content

  • Copy to output directory 更改为Copy always

然后在你的代码中:

try
{
    var configurationMap = new ExeConfigurationFileMap
    {
        // assuming name of your file is "ClassiLibrary1.config"
        ExeConfigFilename = Path.Combine(exeConfigPath, "ClassLibrary1.config")
    };

    config = ConfigurationManager.OpenMappedExeConfiguration(configurationMap, ConfigurationUserLevel.None);
}

【讨论】:

  • 仅供参考,我的配置文件存在于我的类库项目中。我错过了同样的事情。我已经为此编辑了我的问题。
  • @ManojNaik 将配置添加到调用应用程序,而不是类库。类库被调用应用程序消费,这是上下文,所以配置也应该在消费应用程序的上下文中
  • @haim770 我已经使用了您建议的解决方案,但仍然为空。只是想知道,利亚姆的建议是否正确?如果是,那么是否有任何替代方案?
  • @ManojNaik,他当然是对的。客户端配置应该是您的默认配置。我假设您有充分的理由将配置嵌入到类库中。
  • @ManojNaik,您是否按照我在修改后的答案中描述的所有步骤(包括重命名)?您是否验证了Path.Combine(exeConfigPath, "ClassLibrary1.config") 的实际值对应于正确的路径?
猜你喜欢
  • 2019-08-01
  • 1970-01-01
  • 2012-03-20
  • 2013-06-28
  • 2013-07-08
  • 1970-01-01
  • 1970-01-01
  • 2013-01-13
  • 1970-01-01
相关资源
最近更新 更多