/// <summary> /// Registers a URL protocol in the current system. /// </summary> /// <param name="target">The application being integrated.</param> /// <param name="urlProtocol">The URL protocol to register.</param> /// <param name="machineWide">Register the URL protocol machine-wide instead of just for the current user.</param> /// <param name="iconStore">Stores icon files downloaded from the web as local files.</param> /// <param name="accessPoint">Indicates that the handler shall become the default handler for the protocol.</param> /// <exception cref="OperationCanceledException">The user canceled the task.</exception> /// <exception cref="IOException">A problem occurred while writing to the filesystem or registry.</exception> /// <exception cref="WebException">A problem occurred while downloading additional data (such as icons).</exception> /// <exception cref="UnauthorizedAccessException">Write access to the filesystem or registry is not permitted.</exception> public static void Register(FeedTarget target, Model.Capabilities.UrlProtocol urlProtocol, IIconStore iconStore, bool machineWide, bool accessPoint = false) { #region Sanity checks if (urlProtocol == null) { throw new ArgumentNullException(nameof(urlProtocol)); } if (iconStore == null) { throw new ArgumentNullException(nameof(iconStore)); } #endregion using var classesKey = RegistryClasses.OpenHive(machineWide); if (urlProtocol.KnownPrefixes.Count == 0) { if (accessPoint) { // Can only be registered invasively by registering protocol ProgID (will replace existing and become default) using var progIDKey = classesKey.CreateSubKeyChecked(urlProtocol.ID); progIDKey.SetValue("", urlProtocol.Descriptions.GetBestLanguage(CultureInfo.CurrentUICulture) ?? urlProtocol.ID); RegistryClasses.Register(progIDKey, target, urlProtocol, iconStore, machineWide); progIDKey.SetValue(ProtocolIndicator, ""); } } else { // Can be registered non-invasively by registering custom ProgID (without becoming default) using (var progIDKey = classesKey.CreateSubKeyChecked(RegistryClasses.Prefix + urlProtocol.ID)) { progIDKey.SetValue("", urlProtocol.Descriptions.GetBestLanguage(CultureInfo.CurrentUICulture) ?? urlProtocol.ID); progIDKey.SetValue(accessPoint ? RegistryClasses.PurposeFlagAccessPoint : RegistryClasses.PurposeFlagCapability, ""); RegistryClasses.Register(progIDKey, target, urlProtocol, iconStore, machineWide); progIDKey.SetValue(ProtocolIndicator, ""); } if (accessPoint) { foreach (var prefix in urlProtocol.KnownPrefixes) { if (WindowsUtils.IsWindowsVista && !machineWide) { using var userChoiceKey = Registry.CurrentUser.CreateSubKeyChecked($@"{RegKeyUserVistaUrlAssoc}\{prefix.Value}\UserChoice"); userChoiceKey.SetValue("ProgID", RegistryClasses.Prefix + urlProtocol.ID); } else { // Setting default invasively by registering protocol ProgID using var progIDKey = classesKey.CreateSubKeyChecked(prefix.Value); RegistryClasses.Register(progIDKey, target, urlProtocol, iconStore, machineWide); progIDKey.SetValue(ProtocolIndicator, ""); } } } } }
/// <summary> /// Registers an application as a candidate for a default program for some service in the current system. This can only be applied machine-wide, not per user. /// </summary> /// <param name="target">The application being integrated.</param> /// <param name="defaultProgram">The default program information to be registered.</param> /// <param name="iconStore">Stores icon files downloaded from the web as local files.</param> /// <param name="accessPoint">Indicates that the program should be set as the current default for the service it provides.</param> /// <exception cref="OperationCanceledException">The user canceled the task.</exception> /// <exception cref="IOException">A problem occurred while writing to the filesystem or registry.</exception> /// <exception cref="WebException">A problem occurred while downloading additional data (such as icons).</exception> /// <exception cref="UnauthorizedAccessException">Write access to the filesystem or registry is not permitted.</exception> public static void Register(FeedTarget target, Model.Capabilities.DefaultProgram defaultProgram, IIconStore iconStore, bool accessPoint = false) { #region Sanity checks if (defaultProgram == null) { throw new ArgumentNullException(nameof(defaultProgram)); } if (iconStore == null) { throw new ArgumentNullException(nameof(iconStore)); } #endregion using var serviceKey = Registry.LocalMachine.CreateSubKeyChecked($@"{RegKeyMachineClients}\{defaultProgram.Service}"); using (var appKey = serviceKey.CreateSubKeyChecked(defaultProgram.ID)) { appKey.SetValue("", target.Feed.Name); appKey.SetValue(accessPoint ? RegistryClasses.PurposeFlagAccessPoint : RegistryClasses.PurposeFlagCapability, ""); RegistryClasses.Register(appKey, target, defaultProgram, iconStore, machineWide: true); // Set callbacks for Windows SPAD using (var installInfoKey = appKey.CreateSubKeyChecked(RegSubKeyInstallInfo)) { string exePath = Path.Combine(Locations.InstallBase, "0install-win.exe"); installInfoKey.SetValue(RegValueReinstallCommand, new[] { exePath, "integrate", "--machine", "--batch", "--add", "defaults", target.Uri.ToStringRfc() }.JoinEscapeArguments()); installInfoKey.SetValue(RegValueShowIconsCommand, new[] { exePath, "integrate", "--machine", "--batch", "--add", MenuEntry.TagName, "--add", DesktopIcon.TagName, target.Uri.ToStringRfc() }.JoinEscapeArguments()); installInfoKey.SetValue(RegValueHideIconsCommand, new[] { exePath, "integrate", "--machine", "--batch", "--remove", MenuEntry.TagName, "--remove", DesktopIcon.TagName, target.Uri.ToStringRfc() }.JoinEscapeArguments()); installInfoKey.SetValue(RegValueIconsVisible, 0, RegistryValueKind.DWord); } if (defaultProgram.Service == Model.Capabilities.DefaultProgram.ServiceMail) { var mailToProtocol = new Model.Capabilities.UrlProtocol { Verbs = { new Verb { Name = Verb.NameOpen } } }; using var mailToKey = appKey.CreateSubKeyChecked(@"Protocols\mailto"); RegistryClasses.Register(mailToKey, target, mailToProtocol, iconStore, machineWide: true); } } if (accessPoint) { serviceKey.SetValue("", defaultProgram.ID); } }
/// <summary> /// Adds an AutoPlay handler registration to the current system. /// </summary> /// <param name="target">The application being integrated.</param> /// <param name="autoPlay">The AutoPlay handler information to be applied.</param> /// <param name="machineWide">Register the handler machine-wide instead of just for the current user.</param> /// <param name="iconStore">Stores icon files downloaded from the web as local files.</param> /// <param name="accessPoint">Indicates that the handler should become the default handler for all <see cref="Model.Capabilities.AutoPlay.Events"/>.</param> /// <exception cref="OperationCanceledException">The user canceled the task.</exception> /// <exception cref="IOException">A problem occurred while writing to the filesystem or registry.</exception> /// <exception cref="WebException">A problem occurred while downloading additional data (such as icons).</exception> /// <exception cref="UnauthorizedAccessException">Write access to the filesystem or registry is not permitted.</exception> public static void Register(FeedTarget target, Model.Capabilities.AutoPlay autoPlay, IIconStore iconStore, bool machineWide, bool accessPoint = false) { #region Sanity checks if (autoPlay == null) { throw new ArgumentNullException(nameof(autoPlay)); } if (iconStore == null) { throw new ArgumentNullException(nameof(iconStore)); } #endregion string handlerName = RegistryClasses.Prefix + autoPlay.ID; string progId = RegistryClasses.Prefix + "AutoPlay." + autoPlay.ID; using (var classesKey = RegistryClasses.OpenHive(machineWide)) using (var verbKey = classesKey.CreateSubKeyChecked($@"{progId}\shell\{autoPlay.Verb.Name}")) RegistryClasses.Register(verbKey, target, autoPlay.Verb, iconStore, machineWide); var hive = machineWide ? Registry.LocalMachine : Registry.CurrentUser; using (var handlerKey = hive.CreateSubKeyChecked($@"{RegKeyHandlers}\{handlerName}")) { handlerKey.SetValue(accessPoint ? RegistryClasses.PurposeFlagAccessPoint : RegistryClasses.PurposeFlagCapability, ""); handlerKey.SetValue(RegValueProgID, progId); handlerKey.SetValue(RegValueVerb, autoPlay.Verb.Name); handlerKey.SetValue(RegValueProvider, autoPlay.Provider); handlerKey.SetValue(RegValueDescription, autoPlay.Descriptions.GetBestLanguage(CultureInfo.CurrentUICulture) ?? autoPlay.Verb.Name); var icon = autoPlay.GetIcon(Icon.MimeTypeIco) ?? target.Feed.GetBestIcon(Icon.MimeTypeIco, autoPlay.Verb.Command); handlerKey.SetOrDelete(RegValueIcon, icon?.To(x => iconStore.GetFresh(x) + ",0")); } foreach (var autoPlayEvent in autoPlay.Events.Except(x => string.IsNullOrEmpty(x.Name))) { using (var eventKey = hive.CreateSubKeyChecked($@"{RegKeyAssocs}\{autoPlayEvent.Name}")) eventKey.SetValue(handlerName, ""); if (accessPoint) { using var chosenEventKey = hive.CreateSubKeyChecked($@"{RegKeyChosenAssocs}\{autoPlayEvent.Name}"); chosenEventKey.SetValue("", handlerName); } } }
/// <summary> /// Registers a file type in the current system. /// </summary> /// <param name="target">The application being integrated.</param> /// <param name="fileType">The file type to register.</param> /// <param name="machineWide">Register the file type machine-wide instead of just for the current user.</param> /// <param name="iconStore">Stores icon files downloaded from the web as local files.</param> /// <param name="accessPoint">Indicates that the file associations shall become default handlers for their respective types.</param> /// <exception cref="OperationCanceledException">The user canceled the task.</exception> /// <exception cref="IOException">A problem occurred while writing to the filesystem or registry.</exception> /// <exception cref="WebException">A problem occurred while downloading additional data (such as icons).</exception> /// <exception cref="UnauthorizedAccessException">Write access to the filesystem or registry is not permitted.</exception> public static void Register(FeedTarget target, Model.Capabilities.FileType fileType, IIconStore iconStore, bool machineWide, bool accessPoint = false) { #region Sanity checks if (fileType == null) { throw new ArgumentNullException(nameof(fileType)); } if (iconStore == null) { throw new ArgumentNullException(nameof(iconStore)); } #endregion using var classesKey = RegistryClasses.OpenHive(machineWide); // Register ProgID using (var progIDKey = classesKey.CreateSubKeyChecked(RegistryClasses.Prefix + fileType.ID)) { progIDKey.SetValue("", fileType.Descriptions.GetBestLanguage(CultureInfo.CurrentUICulture) ?? fileType.ID); progIDKey.SetValue(accessPoint ? RegistryClasses.PurposeFlagAccessPoint : RegistryClasses.PurposeFlagCapability, ""); RegistryClasses.Register(progIDKey, target, fileType, iconStore, machineWide); } foreach (var extension in fileType.Extensions.Except(x => string.IsNullOrEmpty(x.Value))) { // Register extensions using (var extensionKey = classesKey.CreateSubKeyChecked(extension.Value)) { extensionKey.SetOrDelete(RegValueContentType, extension.MimeType); extensionKey.SetOrDelete(RegValuePerceivedType, extension.PerceivedType); using (var openWithKey = extensionKey.CreateSubKeyChecked(RegSubKeyOpenWith)) openWithKey.SetValue(RegistryClasses.Prefix + fileType.ID, ""); if (accessPoint) { if (!machineWide && WindowsUtils.IsWindowsVista) { // Windows Vista and later store per-user file extension overrides using var overridesKey = Registry.CurrentUser.OpenSubKeyChecked(RegKeyOverrides, writable: true); using var extensionOverrideKey = overridesKey.CreateSubKeyChecked(extension.Value); // Only mess with this part of the registry when necessary bool alreadySet; using (var userChoiceKey = extensionOverrideKey.OpenSubKey("UserChoice", writable: false)) { if (userChoiceKey == null) { alreadySet = false; } else { alreadySet = ((userChoiceKey.GetValue("Progid") ?? "").ToString() == RegistryClasses.Prefix + fileType.ID); } } if (!alreadySet) { // Must delete and recreate instead of direct modification due to wicked ACLs extensionOverrideKey.DeleteSubKeyTree("UserChoice", throwOnMissingSubKey: false); try { using var userChoiceKey = extensionOverrideKey.CreateSubKeyChecked("UserChoice"); userChoiceKey.SetValue("Progid", RegistryClasses.Prefix + fileType.ID); } catch (UnauthorizedAccessException ex) { // Windows may try to prevent modifications to this key Log.Debug(ex); } } } else { extensionKey.SetValue("", RegistryClasses.Prefix + fileType.ID); } } } // Register MIME types if (!string.IsNullOrEmpty(extension.MimeType)) { using var mimeKey = classesKey.CreateSubKeyChecked($@"{RegSubKeyMimeType}\{extension.MimeType}"); mimeKey.SetValue(RegValueExtension, extension.Value); } } }