andrew4582

Register File Type

Aug 7th, 2013
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. /*
  2.  * This file is part of logview4net (logview4net.sourceforge.net)
  3.  * Copyright 2008 Johan Idstam
  4.  *
  5.  *
  6.  * This source code is released under the Artistic License 2.0.
  7.  */
  8. using Microsoft.Win32;
  9.  
  10. namespace logview4net
  11. {
  12.     public class RegisterFiletype
  13.     {
  14.  
  15.         public static bool Register(string appName, string fileExtension, string appDescription, string appPath)
  16.         {
  17.             try
  18.             {
  19.                 if (!fileExtension.StartsWith("."))
  20.                 {
  21.                     fileExtension = "." + fileExtension;
  22.                 }
  23.  
  24.                 // Create a registry key object to represent the HKEY_CLASSES_ROOT registry section
  25.                 RegistryKey rkRoot = Registry.ClassesRoot;
  26.  
  27.                 // Attempt to retrieve the registry key for the XXX file type
  28.                 RegistryKey rkFileType = rkRoot.OpenSubKey(fileExtension);
  29.  
  30.                 // Was the file type found?
  31.                 if (rkFileType == null)
  32.                 {
  33.                     // No, so register it
  34.                     RegistryKey rkNew;
  35.  
  36.                     // Create the registry key
  37.                     rkNew = rkRoot.CreateSubKey(fileExtension);
  38.  
  39.                     // Set the unique file type name
  40.                     rkNew.SetValue("", appName);
  41.  
  42.                     // Create the file type information key
  43.                     RegistryKey rkInfo = rkRoot.CreateSubKey(appName);
  44.  
  45.                     // Set the default value to the file type description
  46.                     rkInfo.SetValue("", appDescription);
  47.  
  48.                     // Create the shell key to contain all verbs
  49.                     RegistryKey rkShell = rkInfo.CreateSubKey("shell");
  50.  
  51.                     // Create a subkey for the "Open" verb
  52.                     RegistryKey rkOpen = rkShell.CreateSubKey("Open");
  53.  
  54.                     // Set the menu name against the key
  55.                     rkOpen.SetValue("", "&Open Session");
  56.  
  57.                     // Create and set the command string
  58.                     rkNew = rkOpen.CreateSubKey("command");
  59.                     rkNew.SetValue("", appPath + " \"%1\"");
  60.  
  61.                     // Assign a default icon
  62.                     rkNew = rkInfo.CreateSubKey("DefaultIcon");
  63.                     rkNew.SetValue("", appPath + ",0");
  64.                 }
  65.                 return true;
  66.             }
  67.             catch
  68.             {
  69.                 return false;
  70.             }
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment