新 Windows C# 构建导致应用丢失已保存的首选项
New Windows C# Build Causes App to Lose Saved Preferences
每次我重新编译 Windows 应用程序时,我都会更新 AssemblyInfo.cs 中的 AssemblyVersion 和 AssemblyFileVersion。
当我的应用程序加载时,我调用
Microsoft.Win32.RegistryKey key1 = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\Classes\MyFileType\shell\open\command", true);
if (key1 == null)
{
Registry.SetValue(@"HKEY_CURRENT_USER\Software\Classes\MyFileType\shell\open\command", null, "\"" + Application.ExecutablePath + "\"" + " \"%1\"");
flag = true;
}
Microsoft.Win32.RegistryKey key2 = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\Classes\.abc", true);
if (key2 == null)
{
Registry.SetValue(@"HKEY_CURRENT_USER\Software\Classes\.abc", null, "MyFileType");
flag = true;
}
Microsoft.Win32.RegistryKey key3 = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\Classes\MyFileType\DefaultIcon", true);
if (key3 == null)
{
Registry.SetValue(@"HKEY_CURRENT_USER\Software\Classes\MyFileType\DefaultIcon", "", Application.ExecutablePath + ",2");
flag = true;
}
if (flag) SHChangeNotify(0x08000000, 0x0000, (IntPtr)null, (IntPtr)null);//SHCNE_ASSOCCHANGED SHCNF_IDLIST
每次我新编译的应用程序运行时,所有保存的首选项都消失了,应用程序恢复为默认首选项。
知道是什么触发了应用程序失去它的首选项吗?是不是因为我更新了版本号?如果是这样,我该如何解决?
是否因为我重新插入了注册表项?
会是什么?
编辑
首选项在应用程序 运行 的实例之间正常加载和保存,直到我进行新的构建...但这里是代码:
private void SaveSettings()
{
Properties.Settings.Default.useLastB = this.lastB.Checked;
Properties.Settings.Default.useLast2B = this.last2B.Checked;
Properties.Settings.Default.useLast3B = this.last3B.Checked;
Properties.Settings.Default.useLastSV = this.lastSV.Checked;
Properties.Settings.Default.useLast2SV = this.last2SV.Checked;
Properties.Settings.Default.useLast3SV = this.last3SV.Checked;
Properties.Settings.Default.showAverage = this.averageBox.Checked;
Properties.Settings.Default.showSum = this.sumBox.Checked;
Properties.Settings.Default.showNormalized = this.normalBox.Checked;
Properties.Settings.Default.pessimistic = this.pessimistic.Checked;
Properties.Settings.Default.realistic = this.realistic.Checked;
Properties.Settings.Default.optimistic = this.optimistic.Checked;
Properties.Settings.Default.useAinsley = this.ainsleyBox.Checked;
Properties.Settings.Default.useHandicapper = this.handicapperBox.Checked;
Properties.Settings.Default.useCustom = this.customBox.Checked;
Properties.Settings.Default.UseMorningLine = this.useMLBox.Checked;
Properties.Settings.Default.SolveAllRacesToSingleFile = this.singleFileCheckBox.Checked;
Properties.Settings.Default.Save();
}
private void LoadSettings()
{
Properties.Settings.Default.Reload();
this.lastB.Checked = Properties.Settings.Default.useLastB;
this.last2B.Checked = Properties.Settings.Default.useLast2B;
this.last3B.Checked = Properties.Settings.Default.useLast3B;
this.lastSV.Checked = Properties.Settings.Default.useLastSV;
this.last2SV.Checked = Properties.Settings.Default.useLast2SV;
this.last3SV.Checked = Properties.Settings.Default.useLast3SV;
this.averageBox.Checked = Properties.Settings.Default.showAverage;
this.sumBox.Checked = Properties.Settings.Default.showSum;
this.normalBox.Checked = Properties.Settings.Default.showNormalized;
this.pessimistic.Checked = Properties.Settings.Default.pessimistic;
this.realistic.Checked = Properties.Settings.Default.realistic;
this.optimistic.Checked = Properties.Settings.Default.optimistic;
this.handicapperBox.Checked = Properties.Settings.Default.useHandicapper;
this.ainsleyBox.Checked = Properties.Settings.Default.useAinsley;
this.customBox.Checked = Properties.Settings.Default.useCustom;
this.useMLBox.Checked = Properties.Settings.Default.UseMorningLine;
this.singleFileCheckBox.Checked = Properties.Settings.Default.SolveAllRacesToSingleFile;
}
谢谢
应用程序设置的默认位置是这个路径:
%USERPROFILE%\Local Settings\Application Data\<Company Name>\<appdomainname>_<eid>_<hash>\<verison>\user.config
请注意,版本 是路径的一部分 - 这就是为什么当您增加版本号时,应用程序似乎已丢失其设置。
您可以覆盖它的存储位置以消除对路径中版本的依赖 - 请参阅 Application Settings Architecture
每次我重新编译 Windows 应用程序时,我都会更新 AssemblyInfo.cs 中的 AssemblyVersion 和 AssemblyFileVersion。
当我的应用程序加载时,我调用
Microsoft.Win32.RegistryKey key1 = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\Classes\MyFileType\shell\open\command", true);
if (key1 == null)
{
Registry.SetValue(@"HKEY_CURRENT_USER\Software\Classes\MyFileType\shell\open\command", null, "\"" + Application.ExecutablePath + "\"" + " \"%1\"");
flag = true;
}
Microsoft.Win32.RegistryKey key2 = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\Classes\.abc", true);
if (key2 == null)
{
Registry.SetValue(@"HKEY_CURRENT_USER\Software\Classes\.abc", null, "MyFileType");
flag = true;
}
Microsoft.Win32.RegistryKey key3 = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\Classes\MyFileType\DefaultIcon", true);
if (key3 == null)
{
Registry.SetValue(@"HKEY_CURRENT_USER\Software\Classes\MyFileType\DefaultIcon", "", Application.ExecutablePath + ",2");
flag = true;
}
if (flag) SHChangeNotify(0x08000000, 0x0000, (IntPtr)null, (IntPtr)null);//SHCNE_ASSOCCHANGED SHCNF_IDLIST
每次我新编译的应用程序运行时,所有保存的首选项都消失了,应用程序恢复为默认首选项。
知道是什么触发了应用程序失去它的首选项吗?是不是因为我更新了版本号?如果是这样,我该如何解决?
是否因为我重新插入了注册表项?
会是什么?
编辑
首选项在应用程序 运行 的实例之间正常加载和保存,直到我进行新的构建...但这里是代码:
private void SaveSettings()
{
Properties.Settings.Default.useLastB = this.lastB.Checked;
Properties.Settings.Default.useLast2B = this.last2B.Checked;
Properties.Settings.Default.useLast3B = this.last3B.Checked;
Properties.Settings.Default.useLastSV = this.lastSV.Checked;
Properties.Settings.Default.useLast2SV = this.last2SV.Checked;
Properties.Settings.Default.useLast3SV = this.last3SV.Checked;
Properties.Settings.Default.showAverage = this.averageBox.Checked;
Properties.Settings.Default.showSum = this.sumBox.Checked;
Properties.Settings.Default.showNormalized = this.normalBox.Checked;
Properties.Settings.Default.pessimistic = this.pessimistic.Checked;
Properties.Settings.Default.realistic = this.realistic.Checked;
Properties.Settings.Default.optimistic = this.optimistic.Checked;
Properties.Settings.Default.useAinsley = this.ainsleyBox.Checked;
Properties.Settings.Default.useHandicapper = this.handicapperBox.Checked;
Properties.Settings.Default.useCustom = this.customBox.Checked;
Properties.Settings.Default.UseMorningLine = this.useMLBox.Checked;
Properties.Settings.Default.SolveAllRacesToSingleFile = this.singleFileCheckBox.Checked;
Properties.Settings.Default.Save();
}
private void LoadSettings()
{
Properties.Settings.Default.Reload();
this.lastB.Checked = Properties.Settings.Default.useLastB;
this.last2B.Checked = Properties.Settings.Default.useLast2B;
this.last3B.Checked = Properties.Settings.Default.useLast3B;
this.lastSV.Checked = Properties.Settings.Default.useLastSV;
this.last2SV.Checked = Properties.Settings.Default.useLast2SV;
this.last3SV.Checked = Properties.Settings.Default.useLast3SV;
this.averageBox.Checked = Properties.Settings.Default.showAverage;
this.sumBox.Checked = Properties.Settings.Default.showSum;
this.normalBox.Checked = Properties.Settings.Default.showNormalized;
this.pessimistic.Checked = Properties.Settings.Default.pessimistic;
this.realistic.Checked = Properties.Settings.Default.realistic;
this.optimistic.Checked = Properties.Settings.Default.optimistic;
this.handicapperBox.Checked = Properties.Settings.Default.useHandicapper;
this.ainsleyBox.Checked = Properties.Settings.Default.useAinsley;
this.customBox.Checked = Properties.Settings.Default.useCustom;
this.useMLBox.Checked = Properties.Settings.Default.UseMorningLine;
this.singleFileCheckBox.Checked = Properties.Settings.Default.SolveAllRacesToSingleFile;
}
谢谢
应用程序设置的默认位置是这个路径:
%USERPROFILE%\Local Settings\Application Data\<Company Name>\<appdomainname>_<eid>_<hash>\<verison>\user.config
请注意,版本 是路径的一部分 - 这就是为什么当您增加版本号时,应用程序似乎已丢失其设置。
您可以覆盖它的存储位置以消除对路径中版本的依赖 - 请参阅 Application Settings Architecture