Wpf - Get values ​​from application configuration file

How to get values ​​from App.Config.

the 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 a null value. The configuration file is in the same project.

+6
wpf configurationmanager
source share
2 answers

If you expand the Properties section in Visual Studio and double-click the settings section, you can add custom settings that end in the configuration file like this:

 <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> 

What you can do this in your code:

 string thing = Properties.Settings.Default.FilePath; 

This is good because it also gives you type safety.

+17
source share

The code you wrote should work - make sure you don't change the "BuildAction" of the configuration file.

+1
source share

All Articles