alejandro carnero
alejandro carnero

Reputation: 1784

Saving and reading user setting in app.config

I have this 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>

I have set system.configuration in the header and in reference, and use this code to save values :

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

but when i try to read these values , nothing is saved :

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

What i m doing wrong

Upvotes: 9

Views: 12446

Answers (1)

Martin
Martin

Reputation: 16423

It could be that you are not calling the Save method to actually persist the values into the configuration file.

After you set the values of the settings, try using:

Properties.Settings.Default.Save();

It's also worth noting that if you are debugging\running in Visual Studio, the config file will get overwritten each time you perform a new build - so updated settings won't be preserved between runs of the application.

Upvotes: 8

Related Questions