private static bool CanAddFileAssociation(FileAssociation fileAssociation) { try { using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Classes")) { using (RegistryKey key2 = key.OpenSubKey(fileAssociation.Extension)) { using (RegistryKey key3 = key.OpenSubKey(fileAssociation.ProgID)) { if ((key2 != null) || (key3 != null)) { Logger.AddWarningInformation(string.Format(CultureInfo.CurrentUICulture, Resources.GetString("SkippedFileAssoc"), new object[] { fileAssociation.Extension })); Logger.AddInternalState("File association for " + fileAssociation.Extension + " skipped, since another application is using it."); return false; } } } } } catch (SecurityException exception) { Logger.AddInternalState("Exception reading registry key : " + exception.StackTrace); Logger.AddInternalState("File association for " + fileAssociation.Extension + " skipped"); return false; } return true; }
private static void AddFileAssociation(FileAssociation fileAssociation, DefinitionIdentity subId, Uri deploymentProviderUri) { if (CanAddFileAssociation(fileAssociation)) { string str = Guid.NewGuid().ToString("B"); string str2 = subId.ToString(); using (RegistryKey key = Registry.CurrentUser.CreateSubKey(@"Software\Classes")) { using (RegistryKey key2 = key.CreateSubKey(fileAssociation.Extension)) { key2.SetValue(null, fileAssociation.ProgID); key2.SetValue("AppId", str2); key2.SetValue("Guid", str); key2.SetValue("DeploymentProviderUrl", deploymentProviderUri.AbsoluteUri); } using (RegistryKey key3 = key.CreateSubKey(fileAssociation.ProgID)) { key3.SetValue(null, fileAssociation.Description); key3.SetValue("AppId", str2); key3.SetValue("Guid", str); key3.SetValue("DeploymentProviderUrl", deploymentProviderUri.AbsoluteUri); using (RegistryKey key4 = key3.CreateSubKey("shell")) { key4.SetValue(null, "open"); using (RegistryKey key5 = key4.CreateSubKey(@"open\command")) { key5.SetValue(null, "rundll32.exe dfshim.dll, ShOpenVerbExtension " + str + " %1"); Logger.AddInternalState("File association created. Extension=" + fileAssociation.Extension + " value=rundll32.exe dfshim.dll, ShOpenVerbExtension " + str + " %1"); } using (RegistryKey key6 = key3.CreateSubKey(@"shellex\IconHandler")) { key6.SetValue(null, str); Logger.AddInternalState("File association icon handler created. Extension=" + fileAssociation.Extension + " value=" + str); } } } using (RegistryKey key7 = key.CreateSubKey("CLSID")) { using (RegistryKey key8 = key7.CreateSubKey(str)) { key8.SetValue(null, "Shell Icon Handler For " + fileAssociation.Description); key8.SetValue("AppId", str2); key8.SetValue("DeploymentProviderUrl", deploymentProviderUri.AbsoluteUri); key8.SetValue("IconFile", fileAssociation.DefaultIcon); using (RegistryKey key9 = key8.CreateSubKey("InProcServer32")) { key9.SetValue(null, "dfshim.dll"); key9.SetValue("ThreadingModel", "Apartment"); } } } } } }
private static string RemoveFileAssociationProgIDInfo(FileAssociation fileAssociation, DefinitionIdentity subId, RegistryKey classesKey, string productName) { string str = null; using (RegistryKey key = classesKey.OpenSubKey(fileAssociation.ProgID, true)) { if (key == null) { return null; } object obj2 = key.GetValue("AppId"); if (!(obj2 is string)) { return null; } string a = (string) obj2; if (!string.Equals(a, subId.ToString(), StringComparison.Ordinal)) { return null; } str = (string) key.GetValue("Guid"); try { classesKey.DeleteSubKeyTree(fileAssociation.ProgID); } catch (ArgumentException exception) { throw new DeploymentException(ExceptionTypes.InvalidARPEntry, string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_FileAssocProgIdDeleteFailed"), new object[] { fileAssociation.ProgID, productName }), exception); } } return str; }
private static void RemoveFileAssociationExtentionInfo(FileAssociation fileAssociation, DefinitionIdentity subId, RegistryKey classesKey, string productName) { using (RegistryKey key = classesKey.OpenSubKey(fileAssociation.Extension, true)) { if (key != null) { object obj2 = key.GetValue("AppId"); if (obj2 is string) { string a = (string) obj2; if (string.Equals(a, subId.ToString(), StringComparison.Ordinal)) { try { classesKey.DeleteSubKeyTree(fileAssociation.Extension); } catch (ArgumentException exception) { throw new DeploymentException(ExceptionTypes.InvalidARPEntry, string.Format(CultureInfo.CurrentUICulture, Resources.GetString("Ex_FileAssocExtDeleteFailed"), new object[] { fileAssociation.Extension, productName }), exception); } } } } } }
private static void RemoveFileAssociation(FileAssociation fileAssociation, DefinitionIdentity subId, string productName) { using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Classes", true)) { if (key != null) { Logger.AddMethodCall("RemoveFileAssociation(" + fileAssociation.ToString() + ") called."); RemoveFileAssociationExtentionInfo(fileAssociation, subId, key, productName); string clsIdString = RemoveFileAssociationProgIDInfo(fileAssociation, subId, key, productName); if (clsIdString != null) { RemoveFileAssociationCLSIDInfo(fileAssociation, subId, key, clsIdString, productName); } } } }