public static void UnregisterFileType(string fileType, bool unregMediaType) { KnownFileTypeInfo info = new KnownFileTypeInfo(fileType, true, true); if (info.IsValid) { if (unregMediaType) { // ==== Unregister media type ==== using (RegistryKey mediaTypeKey = Registry.ClassesRoot.CreateSubKey(info.MediaType)) { if (mediaTypeKey != null) { mediaTypeKey.SetValue("", ""); // ==== Unregister icon ==== using (RegistryKey defaultIconKey = mediaTypeKey.CreateSubKey("DefaultIcon")) { if (defaultIconKey != null) { defaultIconKey.SetValue("", ""); } } mediaTypeKey.DeleteSubKeyTree("shell", false); } } } } }
public static void RegisterFileType(string fileType, bool regMediaType) { KnownFileTypeInfo info = new KnownFileTypeInfo(fileType, true, false); if (info.IsValid) { if (regMediaType) { // ==== Register media type ==== using (RegistryKey mediaTypeKey = Registry.ClassesRoot.CreateSubKey(info.MediaType)) { if (mediaTypeKey != null) { mediaTypeKey.SetValue("", info.MediaType); } // ==== Register icon ==== using (RegistryKey defaultIconKey = mediaTypeKey.CreateSubKey("DefaultIcon")) { if (defaultIconKey != null) { string newValue = string.Format(@"{0}\Resources\{1}.ico", AppConfig.InstallationPath, info.KnownFileType); defaultIconKey.SetValue("", newValue); } } if (File.Exists(info.LaunchPath)) { using (RegistryKey shellKey = mediaTypeKey.CreateSubKey("shell")) { if (shellKey != null) { // ==== Change default action to OPEN ==== shellKey.SetValue("", "open"); // ==== Update OPEN action command ==== using (RegistryKey key = shellKey.CreateSubKey("open\\command")) { if (key != null) { key.SetValue("", string.Format("\"{0}\" launch \"%L\"", info.LaunchPath)); } } } } } else { mediaTypeKey.DeleteSubKeyTree("shell", false); } } } } }
public static bool IsFileTypeRegistered(string fileType) { try { KnownFileTypeInfo info = new KnownFileTypeInfo(fileType, false, false); string keyPath = string.Format("{0}\\shell\\open\\command", info.MediaType); string expectedOpenCommand = string.Format("\"{0}\" launch \"%L\"", info.LaunchPath); using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(keyPath)) { if (key != null) { string openCommand = key.GetValue("") as string; return(openCommand == expectedOpenCommand); } } } catch { } return(false); }
public static bool IsFileTypeRegistered(string fileType) { try { KnownFileTypeInfo info = new KnownFileTypeInfo(fileType, false, false); string keyPath = string.Format("{0}\\shell\\open\\command", info.MediaType); string expectedOpenCommand = string.Format("\"{0}\" launch \"%L\"", info.LaunchPath); using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(keyPath)) { if (key != null) { string openCommand = key.GetValue("") as string; return (openCommand == expectedOpenCommand); } } } catch { } return false; }