hi,

I have developed a form application that contains a lot of user specified settings that need to be saved. Currently the app is using serialisation/deserialisation to manage and save these properties/settings.

Are there any other/better methods/techniques to manage user settings without using a database?

Recommended Answers

All 2 Replies

The ConfigurationManager class! You can also use the designer generated application settings file. If you use the designer generated file be sure to handle upgrades by adding a value called "upgraded" with a default value of false. When you initialize the application call an upgrade (in Program.cs before Application.Run():

if (!Properties.Settings.Default.upgraded)
      {
        Properties.Settings.Default.Upgrade();
        Properties.Settings.Default.upgraded = true;
        Properties.Settings.Default.Save();
      }

Or you can do this yourself. First lets strongly name the config options:

/// <summary>
  /// Gives stringly typed names to configuration options in the xml config
  /// </summary>
  internal enum VSSConfigurationOption
  {
    LoggingEnabled,
    ConnectionString,
    EventsToMonitor,
    HasNotPushedInXMinutes,
    HasNotPushedInXMinutesRepeatInterval, //amount of time to wait before re-logging the event
    ServerData
  }

Loading the configuration options:

private static Configuration config;
    ....
    public static void ReloadConfig()
    {
      lock (configLock)
      {
        if (config != null)
          ConfigurationManager.RefreshSection(ConfigSectionName);

        config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        CheckFields();

        LoggingEnabled = config.AppSettings.Settings[VSSConfigurationOption.LoggingEnabled.ToString()].Value.Equals("Y");
        ConnectionString = config.AppSettings.Settings[VSSConfigurationOption.ConnectionString.ToString()].Value;

Nobody likes nulls, so lets handle them:

/// <summary>
    /// Adds XML elements for configuration elements.
    /// I use this to avoid dealing with null return values.
    /// </summary>
    /// <param name="config"></param>
    private static void CheckFields()
    {
      string[] values = Enum.GetNames(typeof(VSSConfigurationOption));
      foreach (string value in values)
      {
        if (config.AppSettings.Settings[value] == null)
          config.AppSettings.Settings.Add(value, string.Empty);
      }
    }

Save:

/// <summary>
    /// Saves settings to disk
    /// </summary>
    public static void Save()
    {
      config.AppSettings.Settings[VSSConfigurationOption.LoggingEnabled.ToString()].Value = (LoggingEnabled ? "Y" : "N");
      config.AppSettings.Settings[VSSConfigurationOption.ConnectionString.ToString()].Value = ConnectionString;
      config.AppSettings.Settings[VSSConfigurationOption.EventsToMonitor.ToString()].Value = _eventIdsToMonitor;
      config.AppSettings.Settings[VSSConfigurationOption.HasNotPushedInXMinutes.ToString()].Value = HasNotPushedInXMinutes.ToString();
      config.AppSettings.Settings[VSSConfigurationOption.HasNotPushedInXMinutesRepeatInterval.ToString()].Value = HasNotPushedInXMinutes.ToString();
      SaveConfigurationToDisk();
    }
    private static void SaveConfigurationToDisk()
    {
     if (config.HasFile)
        config.Save(ConfigurationSaveMode.Modified);
      else
        config.Save(ConfigurationSaveMode.Full, true); //required if file does not exist. Throws an exception if it does
    }

Its probably a lot easier to use the designer generated file but they both have their drawbacks and benefits.

commented: This could end up as a snippet: "How to store app settings"! +7

great, i will look in to it.
thnx a lot (Y)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.