zoukankan      html  css  js  c++  java
  • FileRegistrationHelper

    FileRegistrationHelper

     public class FileRegistrationHelper 
       
    { 
           
    public static void SetFileAssociation(string extension, string progID) 
           
    { 
               
    // Create extension subkey 
               
    SetValue(Registry.ClassesRoot, extension, progID); 
     
               
    // Create progid subkey 
               
    string assemblyFullPath = System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", @"\"); 
                StringBuilder sbShellEntry = new StringBuilder(); 
                sbShellEntry.AppendFormat("
    \"{0}\" \"%1\"", assemblyFullPath); 
               
    SetValue(Registry.ClassesRoot, progID + @"\shell\open\command", sbShellEntry.ToString()); 
               
    StringBuilder sbDefaultIconEntry = new StringBuilder(); 
                sbDefaultIconEntry
    .AppendFormat("\"{0}\",0", assemblyFullPath); 
               
    SetValue(Registry.ClassesRoot, progID + @"\DefaultIcon", sbDefaultIconEntry.ToString()); 
     
               
    // Create application subkey 
               
    SetValue(Registry.ClassesRoot, @"Applications\" + Path.GetFileName(assemblyFullPath), "", "NoOpenWith"); 
            } 
     
            private static void SetValue(RegistryKey root, string subKey, object keyValue) 
            { 
                SetValue(root, subKey, keyValue, null); 
            } 
            private static void SetValue(RegistryKey root, string subKey, object keyValue, string valueName) 
            { 
                bool hasSubKey = ((subKey != null) && (subKey.Length > 0)); 
                RegistryKey key = root; 
     
                try 
                { 
                    if (hasSubKey) key = root.CreateSubKey(subKey); 
                    key.SetValue(valueName, keyValue); 
                } 
                finally 
                { 
                    if (hasSubKey && (key != null)) key.Close(); 
                } 
            } 
        } 

     

     

     

    string extension = ".def"; 
               
    string title = "something here"; 
               
    string extensionDescription = "some description"; 
               
    FileRegistrationHelper.SetFileAssociation( 
                  extension
    , title + "." + extensionDescription); 

  • 相关阅读:
    BZOJ1912 [Apio2010]patrol 巡逻
    BZOJ2432 [Noi2011]兔农
    BZOJ1010 [HNOI2008]玩具装箱toy
    BZOJ3240 [Noi2013]矩阵游戏
    洛谷【P1303】A*B Problem
    洛谷【2142】高精度减法
    洛谷【P1100】高低位交换
    OIer应该知道的二进制知识
    洛谷【P3908】异或之和
    洛谷【P2431】正妹吃月饼
  • 原文地址:https://www.cnblogs.com/hdl217/p/1872983.html
Copyright © 2011-2022 走看看