C# 如何使用配置管理器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19232695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use ConfigurationManager
提问by gbk
I want to use App.config for storing some setting. I tried to use the next code for getting a parameter from a config file.
我想使用 App.config 来存储一些设置。我尝试使用下一个代码从配置文件中获取参数。
private string GetSettingValue(string paramName)
{
return String.Format(ConfigurationManager.AppSettings[paramName]);
}
I also added System.Configuration
for it (I used a separate class), and in App.config file I have:
我还System.Configuration
为它添加了(我使用了一个单独的类),并且在 App.config 文件中我有:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key ="key1" value ="Sample" />
</appSettings>
</configuration>
But I got an error while trying to use ConfigurationManager
- ConfigurationManager can't exist in such context
, but I already added System.Configuration
. Or did I miss something?
但是我在尝试使用ConfigurationManager
- 时出错ConfigurationManager can't exist in such context
,但我已经添加了System.Configuration
. 还是我错过了什么?
EDIT:
编辑:
class with config (full view)
带配置的类(完整视图)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
namespace browser
{
class ConfigFile
{
private string GetSettingValue(string paramName)
{
return String.Format(ConfigurationManager.AppSettings[paramName]);
}
}
}
EDIT2
编辑2
Add how it looks
添加它的外观
This means the problem is not during using ConfigurationManger
but before - the program "says" that it "doesn't know such element" as I understand the error - the "Element ConfigurationManager" doesn't exist in such context"
这意味着问题不在使用期间,ConfigurationManger
而是在使用之前 - 程序“说”它“不知道这样的元素”,因为我理解错误 - “元素配置管理器”不存在于这种上下文中”
EDIT3
编辑3
EDIT 4
编辑 4
采纳答案by Mike Perrenoud
Okay, it took me a while to see this, but there's no way this compiles:
好的,我花了一段时间才看到这个,但无法编译:
return String.(ConfigurationManager.AppSettings[paramName]);
You're not even calling a method on the String
type. Just do this:
你甚至没有在String
类型上调用方法。只需这样做:
return ConfigurationManager.AppSettings[paramName];
The AppSettings
KeyValuePair already returns a string. If the name doesn't exist, it will return null
.
该AppSettings
KeyValuePair已经返回一个字符串。如果名称不存在,它将返回null
。
Based on your edit you have not yet added a Referenceto the System.Configuration
assembly for the project you're working in.
根据您的编辑,您尚未为您正在处理的项目的程序集添加引用System.Configuration
。
回答by user3093469
I found some answers, but I don't know if it is the right way.This is my solution for now. Fortunatelly it didn′t broke my design mode.
我找到了一些答案,但我不知道这是否正确。这是我现在的解决方案。幸运的是它没有打破我的设计模式。
`
/// <summary>
/// set config, if key is not in file, create
/// </summary>
/// <param name="key">Nome do parametro</param>
/// <param name="value">Valor do parametro</param>
public static void SetConfig(string key, string value)
{
var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var settings = configFile.AppSettings.Settings;
if (settings[key] == null)
{
settings.Add(key, value);
}
else
{
settings[key].Value = value;
}
configFile.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
}
/// <summary>
/// Get key value, if not found, return null
/// </summary>
/// <param name="key"></param>
/// <returns>null if key is not found, else string with value</returns>
public static string GetConfig(string key)
{
return ConfigurationManager.AppSettings[key];
}`
回答by Luis Armando Alquichire
Go to tools
>> nuget
>> console
and type:
转到tools
>> nuget
>>console
并键入:
Install-Package System.Configuration.ConfigurationManager
If you want a specific version:
如果你想要一个特定的版本:
Install-Package System.Configuration.ConfigurationManager -Version 4.5.0
Your ConfigurationManager
dllwill now be imported and the code will begin to work.
您的ConfigurationManager
dll现在将被导入,代码将开始工作。