Getting app settings after name change?

I have a Windows Forms Application that contains settings for each user. I am using Properties.Settings.Default.Upgrade () to keep these settings between versions and everything is fine. I recently had to change the exe name of my application, so after updating all settings will revert to default.

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

System.ConfigurationManager has several methods for opening configuration files other than the current application (e.g. System.Configuration.ConfigurationManager.OpenMappedExeConfiguration), but they all seem to open specific configuration files.

I want to read the settings for each user and they are buried in obscure places in the LocalSettings folder, so I don't know what to pass to these methods.

0


source to share


1 answer


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

In the main method
    // Only check for updates if we have no settings if (string.IsNullOrEmpty (Properties.Settings.Default.Categories)) {UpgradeAppSettings (); }



Using 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);
     }
  }

      

0


source







All Articles