private static void CheckFileExtension_ContainsProgid(IssueTracker Issues, bool Install, string ThisApplicationProgid, string Extension) { // For our association, we want to add our progid to the HCKU extension's OpenWithProgids. // So HKCU\software\classes\.jpg\openwithprogids // -> "Progid" : (empty string) // Putting it in "jpegfile" doesn't work. // Using a custom verb has issues with the open with menu and is awkward. So this is probably still a better solution. var Key = Registry.CurrentUser.OpenSubKey($@"software\classes\{Extension}", writable: Install); if (Key == null) { Issues.Issue($"HKCU key for {Extension} doesn't exist."); if (Install) { Key = Registry.CurrentUser.CreateSubKey($@"software\classes\{Extension}"); Issues.Info($"Created extension \"{Extension}\" in HKCU."); } else { return; } } Debug.Assert(Key != null); var Key_OpenWithProgids = Key.OpenSubKey("openwithprogids", writable: Install); if (Key_OpenWithProgids == null) { Issues.Issue($"HKCU key for {Extension} doesn't have OpenWithProgids."); if (Install) { Key_OpenWithProgids = Key.CreateSubKey("OpenWithProgids"); Issues.Info($"Created OpenWithProgids in \"{Extension}\"."); } else { return; } } Debug.Assert(Key_OpenWithProgids != null); bool HaveProgid = Key_OpenWithProgids.GetValueNames().Any(x => Util.EqualsOICSafe(x, ThisApplicationProgid)); if (!HaveProgid) { Issues.Issue($"HKCU key for {Extension} doesn't contain Progid {ThisApplicationProgid} in OpenWithProgids."); if (Install) { Key_OpenWithProgids.SetValue(ThisApplicationProgid, string.Empty, RegistryValueKind.String); Issues.Info($"Added Progid in \"{Extension}\": \"{ThisApplicationProgid}\"."); } } }
// It's retarded. public static void CheckFileAssociations(IssueTracker Issues, string ApplicationPath, string FriendlyAppName, bool Install) { // Info: The default verb is the default value of the "shell" key. If it's not set, it takes the first subkey alphabetically, which is often "open". Issues.Info("==========================================================================================================================="); Issues.Info("File Handler Verbs:"); CheckFileHandlerVerbs(Issues, "jpegfile"); CheckFileHandlerVerbs(Issues, "pjpegfile"); CheckFileHandlerVerbs(Issues, "pngfile"); CheckFileHandlerVerbs(Issues, "Paint.Picture"); Issues.Info("==========================================================================================================================="); Issues.Info("Extensions (System Defaults):"); foreach (var Info in FileAssocationExtensionInfo) { CheckFileExtension_VerifySystemDefaults(Issues, Info.ExpectedPerceivedType, Info.Extension, Info.ExpectedSystemFileHandler); } if (ApplicationPath != null) { Issues.Info("==========================================================================================================================="); Issues.Info($"HKCU Progid:"); if (ApplicationPath.IndexOf('"') != -1) { throw new ArgumentOutOfRangeException(nameof(ApplicationPath)); } string CommandLine = $"\"{ApplicationPath}\" \"%1\""; CheckProgidKey(Issues, Install, ThisApplicationProgid, CommandLine, FriendlyAppName); } else if (Install) { throw new ArgumentNullException(nameof(ApplicationPath)); } Issues.Info("==========================================================================================================================="); Issues.Info("Extensions (HKCU Progid Registered):"); foreach (var Info in FileAssocationExtensionInfo) { CheckFileExtension_ContainsProgid(Issues, Install, ThisApplicationProgid, Info.Extension); } Issues.Info("==========================================================================================================================="); Issues.Info("User Choice:"); foreach (var Info in FileAssocationExtensionInfo) { CheckUserChoice(Issues, Info.Extension, ThisApplicationProgid); } }
private static void CheckProgidKey(IssueTracker Issues, bool Install, string Progid, string ExpectedCommand, string FriendlyAppName) { // Check that the progid key exists in HKCU classes, and that it has a valid open verb as its only verb. var Key = Registry.CurrentUser.OpenSubKey($@"software\classes\{Progid}", writable: Install); if (Key == null) { Issues.Issue($"ProgidKey {Progid} doesn't exist in HKCU."); if (Install) { Key = Registry.CurrentUser.CreateSubKey($@"software\classes\{Progid}"); Issues.Info($"Created Progid key for {Progid} in HKCU."); } else { return; } } Debug.Assert(Key != null); var Key_Shell = Key.OpenSubKey("shell", writable: Install); if (Key_Shell == null) { Issues.Issue($"ProgidKey {Progid} doesn't have shell in HKCU."); if (Install) { Key_Shell = Key.CreateSubKey("shell"); Issues.Info($"shell subkey created in {Progid}."); } else { return; } } Debug.Assert(Key_Shell != null); foreach (string ShellSubKey in Key_Shell.GetSubKeyNames()) { if (!Util.EqualsOICSafe(ShellSubKey, "open")) { Issues.Issue($"ProgidKey {Progid} contains verb {ShellSubKey}, which isn't expected."); if (Install) { Key_Shell.DeleteSubKeyTree(ShellSubKey); Issues.Info($"Verb \"{ShellSubKey}\" deleted in Progid {Progid}."); } } } var Key_Open = Key_Shell.OpenSubKey("open", writable: Install); if (Key_Open == null) { Issues.Issue($"ProgidKey {Progid} doesn't have an open verb."); if (Install) { Key_Open = Key_Shell.CreateSubKey("open"); Issues.Info($"Created open verb in ProgidKey {Progid}"); } else { return; } } Debug.Assert(Key_Open != null); string CurrentFriendlyAppName = Key_Open.GetValue("FriendlyAppName") as string; if (CurrentFriendlyAppName != FriendlyAppName) { Issues.Issue($"ProgidKey {Progid}'s open verb has incorrect FriendlyAppName \"{CurrentFriendlyAppName}\"; expected \"{FriendlyAppName}\"."); if (Install) { if (FriendlyAppName != null) { Key_Open.SetValue("FriendlyAppName", FriendlyAppName, RegistryValueKind.String); Issues.Info($"FriendlyAppName for {Progid} set to \"{FriendlyAppName}\"."); } else { Key_Open.DeleteValue("FriendlyAppName"); Issues.Info($"FriendlyAppName for {Progid} removed."); } } } var Key_Command = Key_Open.OpenSubKey("command", writable: Install); if (Key_Command == null) { Issues.Issue($"ProgidKey {Progid}'s open verb doesn't have a command subkey."); if (Install) { Key_Command = Key_Open.CreateSubKey("command"); Issues.Info($"Created command in {Progid}'s open verb."); } else { return; } } Debug.Assert(Key_Command != null); string Command = Key_Command.GetValue(null) as string; if (!Util.EqualsOICSafe(Command, ExpectedCommand)) { Issues.Issue($"ProgidKey {Progid}'s open verb command is {{{Command}}}, but {{{ExpectedCommand}}} was expected."); if (Install) { Key_Command.SetValue(null, ExpectedCommand); Issues.Info($"Changed command in {Progid}'s open verb to {{{ExpectedCommand}}}."); } } }