none
Properties.Settings.Default problem RRS feed

  • Question

  • .NET 3.5 Windows Forms application

    I have a program that reads Properties.Settings.Default.MyConnection to set the connectionString value. This value must be set by user after installation to point to correct db file on his system.

    This program works well on 3 different installations on 3 different computers. But it does not work on 4th installation and I really have completely no idea why suddenly on that particular computer it does not work ??

    The connectionString value is entered via a simple interface which basically allows to save a value for a specified key when user clicks Save button:
            private void btnSave_Click(object sender, EventArgs e) {
                try {
                    MyApp.Properties.Settings.Default[tbConfigKey.Text] = tbConfigValue.Text;
                    Properties.Settings.Default.Save();
                }
                catch (Exception ex) {
                    MessageBox.Show(ex.Message);
                    throw ex;
                }
      
    As I said, it works well on 3 installations. But on 4th installation, after properly saving user desired connectionString value, when it opens a connection, it does not read the value entered by user (the value is correct). Instead it always reads the value that was initially set up during installation.
    In my opinion it simply reads:
    MyApp.Properties.Settings.Default.Properties.DefaultValue
    instead of
    MyApp.Properties.Settings.Default.Properties.Name

    I tried to manually change files
    C:\Documents and Settings\user_name\Local settings\Apps\2.0\(...)\myap...exe_(...)\MyApp.exe.config
    and
    C:\Documents and Settings\user_name\Local settings\Apps\2.0\(...)\myap...tion_(...)\MyApp.exe.config
    and set all connection string values to the desired strings but it also did not help.

    Now the situation is strange because ALL connection strings in these 2 configuration files point to the desired location. However, making a simple loop:
                foreach (SettingsProperty x in MyApp.Properties.Settings.Default.Properties) {
                    Console.Writeln(String.Format("Name: {0}\r\nDefaultValue: {1}\r\nValue: {2}\r\n\r\n", x.Name, x.DefaultValue, MyApp.Properties.Settings.Default[x.Name]));
                }
    shows old values!

    Where do the old values come from, if not from configuration files mentioned above?
    Why on a particular installation the program suddenly stopped reading user values?

    I tried to reinstall, reboot, re-enter values, but it is always the same, on this particular computer.



    Sunday, March 23, 2008 9:00 AM

Answers

  • Hi,

     

    the 'old' values (the ones you define at development time) are hard coded. If the franework isn't  able to access or open the config file it will use the defaults instead. This will always happen if you use settings in a dll.

    Since you write that it worked at 3 installations but not on the 4th one, it looks like the application can't access the config file or the config is not valid.

     

    regards

    Philipp

    Sunday, March 23, 2008 6:54 PM

All replies

  • Hi,

     

    the 'old' values (the ones you define at development time) are hard coded. If the franework isn't  able to access or open the config file it will use the defaults instead. This will always happen if you use settings in a dll.

    Since you write that it worked at 3 installations but not on the 4th one, it looks like the application can't access the config file or the config is not valid.

     

    regards

    Philipp

    Sunday, March 23, 2008 6:54 PM
  • Values for user settings come from three places in the following order of precedence:
    (1) user.config, (2) *.exe.config, (3) the compiled exe or dll.

    If this setting is set up as a 'user' setting instead of an 'application' setting, then you need to check the user.config file that is stored (by default) in \Documents and Settings\{user}\Local Settings\Application Data\{app_path}\{version}.  If that file exists, it will override changes that you make to the *.exe.config file.


    This is the file where the code that you give would save a user setting.  Given the symptoms you describe, it may be that it is failing to write to this file, but not failing to read from it.  Check to see if it exists.


    -dwellingbrook
    Thursday, September 24, 2009 6:12 PM