在app.config中保存和读取用户设置

时间:2013-08-01 17:55:32

标签: c# app-config

我有这个app.config:

 <?xml version="1.0"?>
 <configuration>
 <configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
  <section name="Alvaro1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<connectionStrings>
<add name="conexx" connectionString="Data Source=192.168.1.2 ;Initial Catalog =ifdcontroladoria3 ;uid =sa;pwd = admin2012" providerName="System.Data.SqlClient" />
</connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/></startup>
<userSettings>
<Alvaro1.Properties.Settings>
   <setting name="servidor" serializeAs="String">
    <value />
  </setting>
  <setting name="banco" serializeAs="String">
    <value />
  </setting>
  <setting name="user" serializeAs="String">
    <value />
  </setting>
  <setting name="senha" serializeAs="String">
    <value />
  </setting>
</Alvaro1.Properties.Settings>
</userSettings>

我在标题和引用中设置了system.configuration,并使用此代码保存值:

   Properties.Settings.Default.servidor = comboBox1.Text;
   Properties.Settings.Default.banco = cmbBancos.Text;

但是当我尝试读取这些值时,没有保存任何内容:

        servidor = Properties.Settings.Default.servidor;
        banco = Properties.Settings.Default.banco;
        lblLevanta.Text = servidor + " " + banco;

我做错了什么

1 个答案:

答案 0 :(得分:7)

可能是您没有调用Save方法将值实际保存到配置文件中。

设置设置值后,请尝试使用:

Properties.Settings.Default.Save();

还值得注意的是,如果您在Visual Studio中调试\运行,每次执行新构建时都会覆盖配置文件 - 因此在应用程序运行之间不会保留更新的设置。