Geeth
Geeth

Reputation: 5343

wpf - Get values from App Config file

How to get values from 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.

Upvotes: 7

Views: 24257

Answers (2)

Mark
Mark

Reputation: 14930

If you expand the Properties section 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:

<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

Upvotes: 19

veljkoz
veljkoz

Reputation: 8514

The code you wrote should work - make sure you haven't changed 'BuildAction' of the config file.

Upvotes: 1

Related Questions