How to associate a file extension to the current executable in C#

后端 未结 9 2225
故里飘歌
故里飘歌 2020-11-22 12:01

I\'d like to to associate a file extension to the current executable in C#. This way when the user clicks on the file afterwards in explorer, it\'ll run my executable with t

9条回答
  •  没有蜡笔的小新
    2020-11-22 12:52

    The code below is a function the should work, it adds the required values in the windows registry. Usually i run SelfCreateAssociation(".abc") in my executable. (form constructor or onload or onshown) It will update the registy entry for the current user, everytime the executable is executed. (good for debugging, if you have some changes). If you need detailed information about the registry keys involved check out this MSDN link.

    https://msdn.microsoft.com/en-us/library/windows/desktop/dd758090(v=vs.85).aspx

    To get more information about the general ClassesRoot registry key. See this MSDN article.

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms724475(v=vs.85).aspx

    public enum KeyHiveSmall
    {
        ClassesRoot,
        CurrentUser,
        LocalMachine,
    }
    
    /// 
    /// Create an associaten for a file extension in the windows registry
    /// CreateAssociation(@"vendor.application",".tmf","Tool file",@"C:\Windows\SYSWOW64\notepad.exe",@"%SystemRoot%\SYSWOW64\notepad.exe,0");
    /// 
    /// e.g. vendor.application
    /// e.g. .tmf
    /// e.g. Tool file
    /// e.g.  @"C:\Windows\SYSWOW64\notepad.exe"
    /// @"%SystemRoot%\SYSWOW64\notepad.exe,0"
    /// e.g. The user-specific settings have priority over the computer settings. KeyHive.LocalMachine  need admin rights
    public static void CreateAssociation(string ProgID, string extension, string description, string application, string icon, KeyHiveSmall hive = KeyHiveSmall.CurrentUser)
    {
        RegistryKey selectedKey = null;
    
        switch (hive)
        {
            case KeyHiveSmall.ClassesRoot:
                Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(extension).SetValue("", ProgID);
                selectedKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(ProgID);
                break;
    
            case KeyHiveSmall.CurrentUser:
                Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"Software\Classes\" + extension).SetValue("", ProgID);
                selectedKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"Software\Classes\" + ProgID);
                break;
    
            case KeyHiveSmall.LocalMachine:
                Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"Software\Classes\" + extension).SetValue("", ProgID);
                selectedKey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"Software\Classes\" + ProgID);
                break;
        }
    
        if (selectedKey != null)
        {
            if (description != null)
            {
                selectedKey.SetValue("", description);
            }
            if (icon != null)
            {
                selectedKey.CreateSubKey("DefaultIcon").SetValue("", icon, RegistryValueKind.ExpandString);
                selectedKey.CreateSubKey(@"Shell\Open").SetValue("icon", icon, RegistryValueKind.ExpandString);
            }
            if (application != null)
            {
                selectedKey.CreateSubKey(@"Shell\Open\command").SetValue("", "\"" + application + "\"" + " \"%1\"", RegistryValueKind.ExpandString);
            }
        }
        selectedKey.Flush();
        selectedKey.Close();
    }
    
     /// 
        /// Creates a association for current running executable
        /// 
        /// e.g. .tmf
        /// e.g. KeyHive.LocalMachine need admin rights
        /// e.g. Tool file. Displayed in explorer
        public static void SelfCreateAssociation(string extension, KeyHiveSmall hive = KeyHiveSmall.CurrentUser, string description = "")
        {
            string ProgID = System.Reflection.Assembly.GetExecutingAssembly().EntryPoint.DeclaringType.FullName;
            string FileLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
            CreateAssociation(ProgID, extension, description, FileLocation, FileLocation + ",0", hive);
        }
    

提交回复
热议问题