ConfigurationManager读取和写入
提供对客户端应用程序配置文件的访问
通过引入System.Configuration.dll可以用ConfigurationManager类来读取项目中保存在App.config中的配置信息,如:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="Number0" value="9759436"/> <add key="Number1" value="9759437"/> <add key="Number2" value="9759438"/> <add key="Number3" value="9759439"/> </appSettings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
后台:
读取数据:
private void Button_Click_1(object sender, RoutedEventArgs e) { int i = int.Parse(ConfigurationManager.AppSettings["Number2"]); this.txtMsg.Text = i.ToString(); }
写入数据:
Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); cfa.AppSettings.Settings["Number2"].Value = "65434"; cfa.Save();