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