wpf ConfigurationManager.OpenExeConfiguration 方法不返回键值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17276167/
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
ConfigurationManager.OpenExeConfiguration method doesn't return value for keys
提问by mathinvalidnik
I am using the ConfigurationManager.OpenExeConfigurationmethod trying to get to keys from appSettings dynamicaly.I have the file updated dynamicaly so After the updated I am trying to open it and get the new values for the new keys.
我正在使用ConfigurationManager.OpenExeConfiguration方法试图从 appSettings 动态获取密钥。我动态更新了文件,所以更新后我试图打开它并获取新密钥的新值。
Example: In the beggining my file looks like:
示例:在开始我的文件看起来像:
<appSettings>
<add key = "CZH" value = "Chezch Republic"/>
<add key = "DEN" value = "Denmark"/>
</appSettings>
and at some point I am adding a new key/value pair to this section and it looks like that:
在某些时候,我将向此部分添加一个新的键/值对,它看起来像这样:
<appSettings>
<add key = "CZH" value = "Chezch Republic"/>
<add key = "DEN" value = "Denmark"/>
<add key = "ITA" value = "Italy"/>
</appSettings>
So, after the adding I want to get the new value and its key, BUT I can not see a way tha to happen.All that I could get is the AllKeyswhere everything is okay, but I want to have also the value for the new key added.
所以,在添加之后,我想获得新值及其键,但我看不出有什么办法发生。我能得到的只是AllKeys,一切都很好,但我也想拥有添加了新密钥。
I have my file open in Notepad++ and I can see that it is properly updated but I don't know how to get the value for the new key.
我在 Notepad++ 中打开了我的文件,我可以看到它已正确更新,但我不知道如何获取新密钥的值。
EDIT:OK, I'll try to explain it once more and this time I really hope that somenone is going to understand me.
编辑:好的,我会再解释一次,这次我真的希望有人能理解我。
With the ConfigurationManager.OpenExeConfiguration(path_to_the_file)I am loading the configuration file of my application. The return type of the method is : configurationOne of its properties is AllKeysand when I call it, it returns exactly what I need - All the updated keys, BUTI don't know how to get the values for those keys.I am wondering if there is a method or property of the configuration objectthat the ConfigurationManager.OpenExeConfiguration(path_to_the_file)method returns.
使用 ConfigurationManager.OpenExeConfiguration(path_to_the_file)我正在加载我的应用程序的配置文件。该方法的返回类型是:配置它的属性之一是AllKeys,当我调用它时,它返回的正是我需要的 - 所有更新的键,但我不知道如何获取这些键的值。我是不知道是否有所述的方法或属性配置对象,所述ConfigurationManager.OpenExeConfiguration(path_to_the_file)方法返回。
This is all that I am asking.
这就是我要问的全部内容。
回答by user2323308
Try this after adding new value to app.config
在向 app.config 添加新值后尝试此操作
ConfigurationManager.RefreshSection(“appSettings”);
See the article for more details: http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.refreshsection.aspx
有关更多详细信息,请参阅文章:http: //msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.refreshsection.aspx
AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
string ITAValue = appSettings.Settings[“ITA”].Value;
回答by Hannington Mambo
Try this:
尝试这个:
Private Sub readsettings()
Dim mySettings As NameValueCollection = ConfigurationManager.GetSection("ConnectionStrings")
Dim i As Integer = 0
While i < mySettings.Count
Console.WriteLine("#{0} Key: {1} Value: {2}", i, mySettings.GetKey(i), mySettings(i))
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
End While
End Sub
回答by JSJ
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath
See if you are loading the currect file. else wise. use custom file location.
查看您是否正在加载当前文件。否则明智。使用自定义文件位置。
ConfigurationManager.OpenExeConfiguration("myfilepath.exe");
if you are adding removing or changing appsettings on the fly. then i suggest you to use the Fileattribute on appSettingsSections.
如果您要动态添加删除或更改应用程序设置。那么我建议您在appSettings部分使用File属性。

