Click here to Skip to main content
15,886,740 members
Articles / Desktop Programming / WPF

C.B.R.

Rate me:
Please Sign up or sign in to vote.
4.96/5 (52 votes)
22 Oct 2012GPL329 min read 124.3K   1.8K   132  
Comic and electronic publication reader with library management, extended file conversion, and devices support.
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using Microsoft.Win32;

namespace CBR.Core.Helpers
{
	public static class RegisterFiletype
	{
		public static void RegisterToMe(string extension, string description, string iconPath)
		{
			string mainExe = Assembly.GetEntryAssembly().Location;

            Register(extension,
                             description,
			                 '"' + Path.GetFullPath(mainExe) + '"' + " \"%1\"",
                             Path.GetFullPath(iconPath));
		}
		
		public static bool IsRegisteredToMe(string extension)
		{
			string openCommand = GetOpenCommand(extension);
			
			if (string.IsNullOrEmpty(openCommand))
				return false;
			
			string mainExe = Assembly.GetEntryAssembly().Location;
			return openCommand.StartsWith(mainExe) || openCommand.StartsWith('"' + mainExe);
		}
		
		const string explorerFileExts = @"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts";
		
		public static bool IsRegistered(string extension)
		{
			try {
				using (RegistryKey key = Registry.ClassesRoot.OpenSubKey("." + extension)) {
					if (key != null)
						return true;
				}
			} catch (System.Security.SecurityException) {
				// registry access might be denied
			}
			try {
				using (RegistryKey key = Registry.CurrentUser.OpenSubKey(explorerFileExts + "\\." + extension)) {
					if (key != null)
						return true;
				}
			} catch (System.Security.SecurityException) {
				// registry access might be denied
			}
			return false;
		}
		
		static string GetOpenCommand(string extension)
		{
			try {
				string clsKeyName = null;
				using (RegistryKey extKey = Registry.CurrentUser.OpenSubKey(explorerFileExts + "\\." + extension)) {
					if (extKey != null)
						clsKeyName = (string)extKey.GetValue("Progid", "");
				}
				if (string.IsNullOrEmpty(clsKeyName)) {
					using (RegistryKey extKey = Registry.ClassesRoot.OpenSubKey("." + extension)) {
						if (extKey != null)
							clsKeyName = (string)extKey.GetValue("", "");
						else
							return null;
					}
				}
				using (RegistryKey cmdKey = Registry.ClassesRoot.OpenSubKey(clsKeyName + "\\shell\\open\\command")) {
					if (cmdKey != null)
						return (string)cmdKey.GetValue("", "");
					else
						return null;
				}
			} catch (System.Security.SecurityException) {
				// registry access might be denied
				return null;
			}
		}
		
		public static void Register(string extension, string description, string command, string icon)
		{
			try {
				Register(Registry.ClassesRoot, extension, description, command, icon);
			} catch (UnauthorizedAccessException) {
				try {
					Register(Registry.CurrentUser.CreateSubKey("Software\\Classes"), extension, description, command, icon);
				} catch {}
			}
			NotifyShellAfterChanges();
		}
		
		static void Register(RegistryKey rootKey, string extension, string description, string command, string icon)
		{
			RegistryKey extKey, clsKey, openKey;
			extKey = rootKey.CreateSubKey("." + extension);
			// save previous association
			string prev = (string)extKey.GetValue("", "");
			if (prev != "" && prev != ("SD." + extension + "file")) {
				extKey.SetValue("PreSD", extKey.GetValue(""));
			}
			extKey.SetValue("", "SD." + extension + "file");
			extKey.Close();
			
			try {
				extKey = Registry.CurrentUser.OpenSubKey(explorerFileExts + "\\." + extension, true);
				if (extKey != null) {
					extKey.DeleteValue("Progid");
					extKey.Close();
				}
			} catch {}
			
			clsKey = rootKey.CreateSubKey("SD." + extension + "file");
			
			
			clsKey.SetValue("", description);
			clsKey.CreateSubKey("DefaultIcon").SetValue("", '"' + icon + '"');
			openKey = clsKey.CreateSubKey("shell\\open\\command");
			openKey.SetValue("", command);
			openKey.Close();
			clsKey.Close();
		}
		
		public static void UnRegister(string extension)
		{
			UnRegister(extension, Registry.ClassesRoot);
			try
            {
				UnRegister(extension, Registry.CurrentUser.CreateSubKey("Software\\Classes"));
			}
            catch {} // catch CreateSubKey(Software\Classes)-exceptions
			
            NotifyShellAfterChanges();
		}
		
		static void UnRegister(string extension, RegistryKey root)
		{
			try {
				root.DeleteSubKeyTree("SD." + extension + "file");
			} catch {}
			try {
				RegistryKey extKey;
				extKey = root.OpenSubKey("." + extension, true);
				
				// if no association return
				if (extKey == null) return;
				// if other association return too
				if ((string)extKey.GetValue("", "") != ("SD." + extension + "file")) return;
				
				// restore previous association
				string prev = (string)extKey.GetValue("PreSD", "");
				if(prev != "") {
					extKey.SetValue("", prev);
				}
				extKey.Close();
				if (prev != null) {
					root.DeleteSubKeyTree("." + extension);
				}
			} catch {}
		}
		
		[System.Runtime.InteropServices.DllImport("shell32.dll")]
		static extern void SHChangeNotify(int wEventId, int uFlags, IntPtr dwItem1, IntPtr dwItem2);
		
		/// <summary>
		/// Notify Windows explorer that shortcut icons have changed.
		/// </summary>
		static void NotifyShellAfterChanges()
		{
			const int SHCNE_ASSOCCHANGED = 0x08000000;
			const int SHCNF_IDLIST       = 0x0;
			
			SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Architect
France France
WPF and MVVM fan, I practice C # in all its forms from the beginning of the NET Framework without mentioning C ++ / MFC and other software packages such as databases, ASP, WCF, Web & Windows services, Application, and now Core and UWP.
In my wasted hours, I am guilty of having fathered C.B.R. and its cousins C.B.R. for WinRT and UWP on the Windows store.
But apart from that, I am a great handyman ... the house, a rocket stove to heat the jacuzzi and the last one: a wood oven for pizza, bread, and everything that goes inside

https://guillaumewaser.wordpress.com/
https://fouretcompagnie.wordpress.com/

Comments and Discussions