views:

229

answers:

2

I have a Windows forms application that contains per-user settings. I'm using Properties.Settings.Default.Upgrade() to preserve these settings between versions and everything is fine. Recently I had to change the exe name of my application, so after an upgrade, all settings revert to default values.

I guess the settings system thinks it is a different application, so my question is, is there some way to retrieve the settings of another application (the old one)?

System.ConfigurationManager has a few methods for opening config-files other than that of the current application, (i.e.System.Configuration.ConfigurationManager.OpenMappedExeConfiguration) but they all seem to open specific config files.

I want to read per-user settings, and these are buried in obscure locations in the LocalSettings folder, so I wouldn't know what to pass in to these methods.

A: 

I don't know why you want to do this, but these links may help you.

User Settings Persistence in .Net 2.0

Where is the user.config file located in a non-Clickonce app?

Mohammadreza
A: 

For completeness.. I ended up with this hack. Ugly, but it works.

In Main method
// Only do upgrade check if we have no settings if (string.IsNullOrEmpty(Properties.Settings.Default.Categories)) { UpgradeAppSettings(); }

With these methods in the same class.

  private static void UpgradeAppSettings()
  {
     try // Fail silently! This hack is not important enough to cause problems
     {
        List<string> paths = new List<string>();
        paths.Add(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
        paths.Add(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));

        foreach (string path in paths)
        {
           string companypath = System.IO.Directory.GetDirectories(path, "COMPANYNAME").FirstOrDefault();
           if (companypath != null)
           {
              string appfolder = System.IO.Directory.GetDirectories(companypath, "OldName.exe_*").FirstOrDefault();

              if (appfolder != null)
              {
                 // Get the highest version of the app that contains a user.config file
                 var version =
                    System.IO.Directory.GetDirectories(appfolder)
                    .Select(
                       d => new
                       {
                          Path = d,
                          Version = CreateVersion(new System.IO.DirectoryInfo(d).Name),
                          ConfigFile = System.IO.Directory.GetFiles(d, "user.config").FirstOrDefault()
                       }
                    )
                    .Where(v => v.Version != null && v.ConfigFile != null)
                    .OrderByDescending(v => v.Version)
                    .FirstOrDefault();
                 if (version != null)
                 {
                    string text = System.IO.File.ReadAllText(version.ConfigFile);

                    // Change the namespace for serialized categories
                    text = text.Replace(
                       "http://schemas.microsoft.com/clr/nsassem/OldName/OldName",
                       "http://schemas.microsoft.com/clr/nsassem/OldName/NewName");

                    var doc = XDocument.Parse(text);
                    var settings = doc.Descendants("setting");

                    // These are the settings we are interested in
                    ApplySetting(settings, "Categories", s => Properties.Settings.Default.Categories = s);
                    ApplySetting(settings, "ActiveCategoryFilter", s => Properties.Settings.Default.ActiveCategoryFilter = s);
                    ApplySetting(settings, "ActiveCategoryFilterDisplayName", s => Properties.Settings.Default.ActiveCategoryFilterDisplayName = s);
                    ApplySetting(settings, "ListViewLayout", s => Properties.Settings.Default.ListViewLayout = s);
                    ApplySetting(settings, "SplitterSizes", s => Properties.Settings.Default.SplitterSizes = s);
                    ApplySetting(settings, "EditorSizes", s => Properties.Settings.Default.EditorSizes = s);
                    ApplySetting(settings, "WindowStates", s => Properties.Settings.Default.WindowStates = s);
                    ApplySetting(settings, "WindowStates", s => Properties.Settings.Default.WindowStates = s);
                    break;
                 }
              }
           }
        }
     }
     catch { }
  }

  private static void ApplySetting(IEnumerable<XElement> settings, string name, Action<string> action)
  {
     var cat = settings.FirstOrDefault(s => s.Attribute("name") != null && s.Attribute("name").Value == name);
     if (cat != null)
     {
        action(cat.Value);
     }
  }
Robert Jeppesen