AppSettings从.config文件中获取值

Mat*_*att 163 .net c# configurationmanager appsettings

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

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

.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!-- ... -->
    <add key="ClientsFilePath" value="filepath"/>
    <!-- ... -->
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

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

Ada*_*dam 324

这对我有用:

string value = System.Configuration.ConfigurationManager.AppSettings[key];
Run Code Online (Sandbox Code Playgroud)

  • 需要添加引用`System.Configuration`. (35认同)
  • 我试过这个方法,但我得到了空字符串. (12认同)
  • 我的**.NET 4.5**`System.Configuration`没有`ConfigurationManager` (12认同)
  • 我提供的代码要求YourProgram.exe.config文件与YourProgram.exe存在于同一文件夹中.只要确保它在那里.常见错误是将app.config文件复制到目标目录,这将无法正常工作. (5认同)
  • 我在 4.5 项目中也得到了一个空值——在这上面花了两个小时......必须是一个错误。 (3认同)

rot*_*ers 56

dtsg给出的答案有效:

string filePath = ConfigurationManager.AppSettings["ClientsFilePath"];
Run Code Online (Sandbox Code Playgroud)

但你需要添加一个程序集引用

系统配置

转到解决方案资源管理器,右键单击 "引用",然后选择" 添加引用".选择组件选项卡并搜索配置.

参考经理

这是我的App.config的一个例子:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <appSettings>
    <add key="AdminName" value="My Name"/>
    <add key="AdminEMail" value="MyEMailAddress"/>
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

您可以通过以下方式获得:

string adminName = ConfigurationManager.AppSettings["AdminName"];
Run Code Online (Sandbox Code Playgroud)

  • 我似乎和OP有同样的问题.使用VS2015.我不知道如何或为什么,但我对System.Configuration的引用说`System.configuration`(小写). (2认同)

dts*_*tsg 20

放手一搏:

string filePath = ConfigurationManager.AppSettings["ClientsFilePath"];
Run Code Online (Sandbox Code Playgroud)

  • 您的实施必须是不正确的.这绝对适用于从配置文件中检索键值. (4认同)
  • 我试过这个方法,但我得到了空字符串. (2认同)

Mas*_*ali 18

从配置中读取:

您需要添加对Config的引用

  1. 在项目中打开"属性"
  2. 转到"设置"标签
  3. 添加"名称"和"值"
  4. 使用以下代码获取价值:
string value = Properties.Settings.Default.keyname;
Run Code Online (Sandbox Code Playgroud)

保存到配置:

Properties.Settings.Default.keyName = value;
Properties.Settings.Default.Save();
Run Code Online (Sandbox Code Playgroud)

  • 非常有用,因为如果您在项目属性的"设置"选项卡中添加值,则使用ConfigurationManager的所有其他答案都不起作用.谢谢. (3认同)
  • 这对我也有效。我得到了一个带有上述建议的 `null` 字符串,但是 `Properties.Settings.Default.&lt;keyName&gt;` 的效果很好! (2认同)

Kkl*_*loe 7

我在用:

    ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
    //configMap.ExeConfigFilename = @"d:\test\justAConfigFile.config.whateverYouLikeExtension";
    configMap.ExeConfigFilename = AppDomain.CurrentDomain.BaseDirectory + ServiceConstants.FILE_SETTING;
    Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
    value1 = System.Configuration.ConfigurationManager.AppSettings["NewKey0"];
    value2 = config.AppSettings.Settings["NewKey0"].Value;
    value3 = ConfigurationManager.AppSettings["NewKey0"];
Run Code Online (Sandbox Code Playgroud)

其中value1 = ...value3 = ...给出null,value2 = ......有效

然后我决定用以下内容替换内部app.config:

// Note works in service but not in wpf
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"d:\test\justAConfigFile.config.whateverYouLikeExtension");
ConfigurationManager.RefreshSection("appSettings");

string value = ConfigurationManager.AppSettings["NewKey0"];
Run Code Online (Sandbox Code Playgroud)

使用VS2012 .net 4


小智 5

app/web.config文件中设置以下配置:

<configuration>
  <appSettings>
    <add key="NameForTheKey" value="ValueForThisKey" />
    ... 
    ...    
  </appSettings>
...
...
</configuration>
Run Code Online (Sandbox Code Playgroud)

然后您可以在代码中输入以下内容来访问此代码:

string myVar = System.Configuration.ConfigurationManager.AppSettings["NameForTheKey"];
Run Code Online (Sandbox Code Playgroud)

*请注意,此功能适用于.net4.5.x.net4.6.x; 但不工作.net core。最好的问候:拉斐尔