wpf - 从应用配置文件中获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3931830/
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
wpf - Get values from App Config file
提问by Geeth
How to get values from App.Config.
如何从 App.Config 获取值。
Code:
代码:
<configuration>
<appSettings>
<add key="ShowRoomCode" value="1000"/>
<add key="FolderPath" value="D:\Images\Book\"/>
</appSettings>
</configuration>
string imageFolderPath = ConfigurationManager.AppSettings["FolderPath"];
But it returns null value. Config file is in the Same project.
但它返回空值。配置文件在同一个项目中。
回答by Mark
If you expand the Propertiessection of Visual Studio and double click the settings section, you will be able to add custom settings which end up like so in the config file:
如果您展开Visual Studio的属性部分并双击设置部分,您将能够在配置文件中添加自定义设置:
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="WpfApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<WpfApplication1.Properties.Settings>
<setting name="FilePath" serializeAs="String">
<value>Thing</value>
</setting>
</WpfApplication1.Properties.Settings>
</userSettings>
</configuration>
Which you can then do this in your code:
然后您可以在代码中执行此操作:
string thing = Properties.Settings.Default.FilePath;
Which is nice because it gives you type safety too
这很好,因为它也为您提供了类型安全
回答by veljkoz
The code you wrote should work - make sure you haven't changed 'BuildAction' of the config file.
您编写的代码应该可以工作 - 确保您没有更改配置文件的“BuildAction”。