Wrap UpdateManager in using statement + enable methods run as standalone

This commit is contained in:
Jeremy Wu 2020-03-23 18:09:52 +11:00
parent b85f5a147d
commit 86903e5e63

View file

@ -13,12 +13,17 @@ namespace Wox.Core.Configuration
{ {
public class Portable : IPortable public class Portable : IPortable
{ {
private UpdateManager portabilityUpdater; /// <summary>
/// As at Squirrel.Windows version 1.5.2, UpdateManager needs to be disposed after finish
/// </summary>
/// <returns></returns>
private UpdateManager NewUpdateManager()
{
return new UpdateManager(string.Empty, Constant.Wox, Constant.RootDirectory);
}
public void DisablePortableMode() public void DisablePortableMode()
{ {
portabilityUpdater = new UpdateManager(string.Empty, Constant.Wox, Constant.RootDirectory);
try try
{ {
MoveUserDataFolder(DataLocation.PortableDataPath, DataLocation.RoamingDataPath); MoveUserDataFolder(DataLocation.PortableDataPath, DataLocation.RoamingDataPath);
@ -34,13 +39,10 @@ namespace Wox.Core.Configuration
MessageBox.Show("Wox needs to restart to finish disabling portable mode, " + MessageBox.Show("Wox needs to restart to finish disabling portable mode, " +
"after the restart your portable data profile will be deleted and roaming data profile kept"); "after the restart your portable data profile will be deleted and roaming data profile kept");
portabilityUpdater.Dispose();
UpdateManager.RestartApp(); UpdateManager.RestartApp();
} }
catch (Exception e) catch (Exception e)
{ {
portabilityUpdater.Dispose();
#if !DEBUG #if !DEBUG
Log.Exception("Portable", "Error occured while disabling portable mode", e); Log.Exception("Portable", "Error occured while disabling portable mode", e);
#endif #endif
@ -50,8 +52,6 @@ namespace Wox.Core.Configuration
public void EnablePortableMode() public void EnablePortableMode()
{ {
portabilityUpdater = new UpdateManager(string.Empty, Constant.Wox, Constant.RootDirectory);
try try
{ {
MoveUserDataFolder(DataLocation.RoamingDataPath, DataLocation.PortableDataPath); MoveUserDataFolder(DataLocation.RoamingDataPath, DataLocation.PortableDataPath);
@ -67,13 +67,10 @@ namespace Wox.Core.Configuration
MessageBox.Show("Wox needs to restart to finish enabling portable mode, " + MessageBox.Show("Wox needs to restart to finish enabling portable mode, " +
"after the restart your roaming data profile will be deleted and portable data profile kept"); "after the restart your roaming data profile will be deleted and portable data profile kept");
portabilityUpdater.Dispose();
UpdateManager.RestartApp(); UpdateManager.RestartApp();
} }
catch (Exception e) catch (Exception e)
{ {
portabilityUpdater.Dispose();
#if !DEBUG #if !DEBUG
Log.Exception("Portable", "Error occured while enabling portable mode", e); Log.Exception("Portable", "Error occured while enabling portable mode", e);
#endif #endif
@ -83,15 +80,21 @@ namespace Wox.Core.Configuration
public void RemoveShortcuts() public void RemoveShortcuts()
{ {
var exeName = Constant.Wox + ".exe"; using (var portabilityUpdater = NewUpdateManager())
portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.StartMenu); {
portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.Desktop); var exeName = Constant.Wox + ".exe";
portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.Startup); portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.StartMenu);
portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.Desktop);
portabilityUpdater.RemoveShortcutsForExecutable(exeName, ShortcutLocation.Startup);
}
} }
public void RemoveUninstallerEntry() public void RemoveUninstallerEntry()
{ {
portabilityUpdater.RemoveUninstallerRegistryEntry(); using (var portabilityUpdater = NewUpdateManager())
{
portabilityUpdater.RemoveUninstallerRegistryEntry();
}
} }
public void MoveUserDataFolder(string fromLocation, string toLocation) public void MoveUserDataFolder(string fromLocation, string toLocation)
@ -107,10 +110,13 @@ namespace Wox.Core.Configuration
public void CreateShortcuts() public void CreateShortcuts()
{ {
var exeName = Constant.Wox + ".exe"; using (var portabilityUpdater = NewUpdateManager())
portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.StartMenu, false); {
portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.Desktop, false); var exeName = Constant.Wox + ".exe";
portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.Startup, false); portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.StartMenu, false);
portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.Desktop, false);
portabilityUpdater.CreateShortcutsForExecutable(exeName, ShortcutLocation.Startup, false);
}
} }
public void CreateUninstallerEntry() public void CreateUninstallerEntry()
@ -125,7 +131,10 @@ namespace Wox.Core.Configuration
.CreateSubKey(uninstallRegSubKey + "\\" + Constant.Wox, RegistryKeyPermissionCheck.ReadWriteSubTree); .CreateSubKey(uninstallRegSubKey + "\\" + Constant.Wox, RegistryKeyPermissionCheck.ReadWriteSubTree);
key.SetValue("DisplayIcon", Constant.ApplicationDirectory + "\\app.ico", RegistryValueKind.String); key.SetValue("DisplayIcon", Constant.ApplicationDirectory + "\\app.ico", RegistryValueKind.String);
portabilityUpdater.CreateUninstallerRegistryEntry(); using (var portabilityUpdater = NewUpdateManager())
{
portabilityUpdater.CreateUninstallerRegistryEntry();
}
} }
internal void IndicateDeletion(string filePathTodelete) internal void IndicateDeletion(string filePathTodelete)