c# - AppSettings 从 .config 文件获取值

标签 c# .net appsettings configurationmanager

我无法访问配置文件中的值。

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var clientsFilePath = config.AppSettings.Settings["ClientsFilePath"].Value; 
// the second line gets a NullReferenceException

.config 文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!-- ... -->
    <add key="ClientsFilePath" value="filepath"/>
    <!-- ... -->
  </appSettings>
</configuration>

你有什么建议我该怎么办?

最佳答案

这对我有用:

string value = System.Configuration.ConfigurationManager.AppSettings[key];

关于c# - AppSettings 从 .config 文件获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10766654/

相关文章:

c# - 使用 Windows 服务进行多任务处理

c# - 与 .NET 上的 32 位平台相比,64 位平台上的大对象堆碎片是否是一个问题?

c# - 任务异步困惑

c# - SmtpClient.Send随机导致SmtpException 4.7.0等待客户端输入超时

azure - 应用程序设置 - Azure 应用程序服务(多个环境)

.net - 如何在不使用用户设置的情况下在运行时读取/写入 app.config 设置?

c# - App.config 中的更改在重新启动应用程序后不会反射(reflect)出来

c# - 使用 HwndHost 在 WPF 中托管外部窗口的正确方法

.net wrapper for twitter v1.1 API

.net - 当我的代码没有出现在堆栈中时,如何调试此错误?