How to use App.Config in WPF applications

How to use App.Config in WPF applications
When you create a WPF application it doesn't have a config file like ASP.NET websites do, but with a couple of steps you can add one and access it,    here's how.
  1. Rightclick project, add item, choose Application Configuration File and be sure to call it App.config(not App1.config as it suggests).
  2. Add refernce.
  3. open up the App.config file and replace it with this code (case-sensitive):
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <appSettings>
        <add key="xmlDataDirectory" value="c:\testdata"/>
        </appSettings>
    </configuration>
  4. add the using statement:
    using System.Configuration;
  5. then in your code, access the variable like this:
    string xmlDataDirectory = ConfigurationManager.AppSettings.Get("xmlDataDirectory");
 
  1. Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
  2. cfa.AppSettings.Settings["xmlDataDirectory"].Value = "WANGLICHAO";  
  3. cfa.Save();  

你可能感兴趣的:(How to use App.Config in WPF applications)