代码之家  ›  专栏  ›  技术社区  ›  Robert Jeppesen

在名称更改后检索应用程序设置?

  •  0
  • Robert Jeppesen  · 技术社区  · 16 年前

    我有一个包含每个用户设置的Windows窗体应用程序。 我正在使用Properties.Settings.Default.Upgrade()在不同版本之间保留这些设置,一切正常。 最近我不得不更改我的应用程序的exe名称,所以升级后,所有设置都恢复为默认值。

    我猜设置系统认为这是一个不同的应用程序,所以我的问题是,

    我想读取每个用户的设置,这些设置被隐藏在LocalSettings文件夹中的模糊位置,因此我不知道向这些方法传递什么。

    1 回复  |  直到 16 年前
        1
  •  0
  •   Robert Jeppesen    15 年前

    为了完整起见。。 我最终被这个黑客攻击了。很难看,但很管用。

    主要方法
    //只有在没有设置的情况下才进行升级检查 { 升级appsettings(); }

    在同一类中使用这些方法。

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