Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, July 29, 2016

How do you keep user.config settings across different assembly versions in .net?

How do you keep user.config settings across different assembly versions in .net?


Basically the problem is that each time the assembly version changes (i.e. the user installs a new version of the application) all their settings are reset the the defaults (or more accurately a new user.config file is created in a folder with a different version number as the name)

How can I keep the same settings when upgrading versions, since using ini files or the registry seem to be discouraged?

When we used Clickonce it seemed to be able to handle this, so it seems like it should be able to be done, but I'm not sure how.

Answer by Markus Olsson for How do you keep user.config settings across different assembly versions in .net?


ApplicationSettingsBase has a method called Upgrade which migrates all settings from the previous version.

In order to run the merge whenever you publish a new version of your application you can define a boolean flag in your settings file that defaults to true. Name it UpgradeRequired or something similar.

Then, at application start you check to see if the flag is set and if it is, call the Upgrade method, set the flag to false and save your configuration.

if (Settings.Default.UpgradeRequired)  {      Settings.Default.Upgrade();      Settings.Default.UpgradeRequired = false;      Settings.Default.Save();  }  

Read more about the Upgrade method at MSDN. The GetPreviousVersion might also be worth a look if you need to do some custom merging.

Answer by JMD for How do you keep user.config settings across different assembly versions in .net?


If your changes to user.settings are done programmatically, how about maintaining a copy of (just) the modifications to user.settings in a separate file, e.g. user.customized.settings?

You probably still want to maintain and load the modified settings in user.settings as well. But this way when you install a newer version of your application with its newer version of user.settings you can ask the user if they want to continue to use their modified settings by copying them back into the new user.settings. You could import them wholesale, or get fancier and ask the user to confirm which settings they want to continue to use.

EDIT: I read too quickly over the "more accurately" part about assembly versions causing a new user.settings to be installed into a new version-specific directory. Thus, the idea above probably doesn't help you, but may provide some food for thought.

Answer by Ian for How do you keep user.config settings across different assembly versions in .net?


This is how I handled it:

public virtual void LoadSettings(ServiceFileFormBaseSettings settings = null, bool resetSettingsToDefaults = false)  {      if (settings == null)              return;        if (resetSettingsToDefaults)          settings.Reset();      else      {          settings.Reload();            if (settings.IsDefault)              settings.Upgrade();      }        this.Size = settings.FormSize;  

}

and in the settings class, I defined the IsDefault property:

// SaveSettings always sets this to be FALSE.  // This will have the default value TRUE when first deployed, or immediately after an upgrade.  // When the settings exist, this is false.  //  [UserScopedSettingAttribute()]  [DefaultSettingValueAttribute("true")]  public virtual bool IsDefault  {      get { return (bool)this["IsDefault"]; }      set { this["IsDefault"] = value; }  }  

In the SaveSettings, I set IsDefault to false:

public virtual void SaveSettings(ServiceFileFormBaseSettings settings = null)  {      if (settings == null) // ignore calls from this base form, if any          return;        settings.IsDefault = false;      settings.FormSize = this.Size;      settings.Save();  }  

Answer by Taurus for How do you keep user.config settings across different assembly versions in .net?


I know it's been awhile... In a winforms app, just call My.Settings.Upgrade() before you load them. This will get the latest settings, whether the current version or a previous version.

Answer by dotNET for How do you keep user.config settings across different assembly versions in .net?


Here's my research in case anyone else is having a hard time with migrating settings that have been changed/removed. Basic problem is that GetPreviousVersion() does not work if you have renamed or removed the setting in the new version of your application. So you need to keep the setting in your Settings class, but add a few attributes/artifacts to it so that you don't inadvertently use it in the code elsewhere, making it obsolete. A sample obsolete setting would look like this in VB.NET (can easily be translated to C#):

  Public Property OldSettingName() As String    Get      Throw New NotSupportedException("This property is obsolete")    End Get    Set      Throw New NotSupportedException("This property is obsolete")    End Set  End Property  

Make sure you add this property to the same namespace/class that has your application settings. In VB.NET, this class is named MySettings and is available in My namespace. You can use partial class functionality to prevent your obsolete settings from mixing up with your current settings.

Full credit to jsharrison for posting an excellent article about this issue. You can read more details about it there.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.