ËxtensionManager: Added IsAssociationOutdated function

This commit is contained in:
erri120 2019-10-28 15:00:26 +01:00
parent 746837cf26
commit c676aed591
No known key found for this signature in database
GPG Key ID: A8C0A18D8D4D3135

View File

@ -15,18 +15,51 @@ namespace Wabbajack.Common
private static readonly string AppRegPath = "Software\\Classes\\Applications\\Wabbajack.exe";
private static readonly string AppAssocRegPath =
$"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\{Extension}";
private static readonly string Win8RegPath = $"Software\\Classes\\{Extension.Replace(".", "")}_auto_file";
public static bool IsExtensionAssociated()
{
return (Registry.CurrentUser.OpenSubKey(AppAssocRegPath, false) == null);
}
public static void AssociateExtension(string iconPath, string appPath)
private static bool IsWin8()
{
var winVersion = new Version(6, 2, 9200, 0);
return (Environment.OSVersion.Platform >= PlatformID.Win32NT &&
Environment.OSVersion.Version >= winVersion);
}
public static bool IsAssociationOutdated(string currentIconPath, string currentAppPath)
{
var extReg = Registry.CurrentUser.OpenSubKey(ExtRegPath, false);
var appReg = Registry.CurrentUser.OpenSubKey(AppAssocRegPath, false);
var appAssocReg = Registry.CurrentUser.OpenSubKey(AppAssocRegPath, false);
if (extReg == null || appReg == null || appAssocReg == null) return true;
if (extReg.OpenSubKey("DefaultIcon", false) == null) return true;
if (extReg.OpenSubKey("PerceivedType", false) == null) return true;
if (extReg.OpenSubKey("DefaultIcon", false)?.GetValue("").ToString() != currentIconPath) return true;
if (appReg.OpenSubKey("shell\\open\\command", false) == null) return true;
if (appReg.OpenSubKey("DefaultIcon", false) == null) return true;
if (appReg.OpenSubKey("shell\\open\\command", false)?.GetValue("").ToString() != $"\"{currentAppPath}\" -i %i") return true;
if (appReg.OpenSubKey("DefaultIcon", false)?.GetValue("").ToString() != currentIconPath) return true;
if (appAssocReg.OpenSubKey("UserChoice", false) == null) return true;
if (appAssocReg.OpenSubKey("UserChoice", false)?.GetValue("Progid").ToString() !=
"Applications\\Wabbajack.exe") return true;
if (!IsWin8()) return false;
if (extReg.GetValue("").ToString() != Extension.Replace(".", "") + "_auto_file") return true;
var win8FileReg = Registry.CurrentUser.OpenSubKey(Win8RegPath, false);
if (win8FileReg?.OpenSubKey("shell\\open\\command", false) == null) return true;
return win8FileReg.OpenSubKey("shell\\open\\command", false)?.GetValue("").ToString() !=
$"\"{currentAppPath}\" -i %i";
}
public static void AssociateExtension(string iconPath, string appPath)
{
var extReg = Registry.CurrentUser.CreateSubKey(ExtRegPath);
if (Environment.OSVersion.Platform >= PlatformID.Win32NT && Environment.OSVersion.Version >= winVersion)
if (IsWin8())
extReg?.SetValue("", Extension.Replace(".", "") + "_auto_file");
var appReg = Registry.CurrentUser.CreateSubKey(AppRegPath);
var appAssocReg = Registry.CurrentUser.CreateSubKey(AppAssocRegPath);
@ -40,10 +73,8 @@ namespace Wabbajack.Common
appAssocReg?.CreateSubKey("UserChoice")?.SetValue("Progid", "Applications\\Wabbajack.exe");
SHChangeNotify(0x000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
if (Environment.OSVersion.Platform < PlatformID.Win32NT ||
Environment.OSVersion.Version < winVersion) return;
var win8FileReg =
Registry.CurrentUser.CreateSubKey($"Software\\Classes\\{Extension.Replace(".", "")}_auto_file");
if (!IsWin8()) return;
var win8FileReg = Registry.CurrentUser.CreateSubKey(Win8RegPath);
win8FileReg?.CreateSubKey("shell\\open\\command")?.SetValue("", $"\"{appPath}\" -i %i");
}
}