【问题标题】:How to associate a file extension to the current executable in C#如何将文件扩展名关联到 C# 中的当前可执行文件
【发布时间】:2010-09-09 08:16:29
【问题描述】:

我想将文件扩展名与 C# 中的当前可执行文件相关联。 这样,当用户随后在资源管理器中单击文件时,它将以给定文件作为第一个参数运行我的可执行文件。 理想情况下,它还将给定文件扩展名的图标设置为我的可执行文件的图标。 谢谢大家。

【问题讨论】:

  • codeproject.com/KB/dotnet/System_File_Association.aspx 那么csharpfriends.com/Forums/ShowPost.aspx?PostID=32627 可能会有所帮助。如果需要,您可以随时通过 API 直接为关联进行注册表编辑...但由您决定 编辑:还可以阅读那里的 cmets,一些优点,例如使用带空格的路径的引号:@"C:\ SomePath\MyApp.exe ""%1""" 还有注册表访问权限问题,尤其是 Vista...祝你好运:)
  • 这已在以下stack overflow thread 中得到彻底解答。我们一直在使用这个实现并且效果很好。它也是开源的,并集成到 MSBuild 中。
  • 这已在以下线程中得到彻底回答:stackoverflow.com/questions/2993118/…
  • @BlakeNiemyjski 您关于将图标嵌入到 .NET 程序集中的链接如何与这个关于文件扩展名关联的问题相关?
  • 我正在链接到堆栈溢出帖子,其中包含有关通过 Wix 安装程序关联文件扩展名的有用 wix 信息。

标签: c# windows winforms


【解决方案1】:

文件关联在注册表中的 HKEY_CLASSES_ROOT 下定义。

有一个 VB.NET 示例 here,您可以轻松地将其移植到 C#。

【讨论】:

    【解决方案2】:

    似乎没有用于直接管理文件关联的 .Net API,但您可以使用 Registry 类来读取和写入所需的密钥。

    您需要在 HKEY_CLASSES_ROOT 下创建一个密钥,并将名称设置为您的文件扩展名(例如:“.txt”)。将此键的默认值设置为文件类型的唯一名称,例如“Acme.TextFile”。然后在 HKEY_CLASSES_ROOT 下创建另一个键,名称设置为“Acme.TextFile”。添加一个名为“DefaultIcon”的子项,并将该项的默认值设置为包含您希望用于此文件类型的图标的文件。添加另一个名为“shell”的兄弟。在“shell”键下,为您希望通过资源管理器上下文菜单提供的每个操作添加一个键,将每个键的默认值设置为可执行文件的路径,后跟空格和“%1”表示路径到选定的文件。

    例如,这是一个示例注册表文件,用于在 .txt 文件和 EmEditor 之间创建关联:

    Windows 注册表编辑器版本 5.00 [HKEY_CLASSES_ROOT\.txt] @="emeditor.txt" [HKEY_CLASSES_ROOT\emeditor.txt] @="文本文档" [HKEY_CLASSES_ROOT\emeditor.txt\DefaultIcon] @="%SystemRoot%\\SysWow64\\imageres.dll,-102" [HKEY_CLASSES_ROOT\emeditor.txt\shell] [HKEY_CLASSES_ROOT\emeditor.txt\shell\open] [HKEY_CLASSES_ROOT\emeditor.txt\shell\open\command] @="\"C:\\Program Files\\EmEditor\\EMEDITOR.EXE\" \"%1\"" [HKEY_CLASSES_ROOT\emeditor.txt\shell\print] [HKEY_CLASSES_ROOT\emeditor.txt\shell\print\command] @="\"C:\\Program Files\\EmEditor\\EMEDITOR.EXE\" /p \"%1\""

    【讨论】:

    • 有没有办法将一些参数传递给 exe(在本例中是 C:\Program Files\EmEditor\EMEDITOR.EXE)?
    • @Apparao 是的,以打印命令为例。它在文件名之前传递 /p 参数。
    • 我知道这个答案已有十多年的历史了,但仍然 - 我认为这不是将纯文本 (*.txt) 的公共文件格式与随机程序——这基本上是“接管”文件关联,如果至少之前已经有文本文件与某些程序的关联,则覆盖它。微软文档,尽管它是不令人满意的,但至少指出将程序与公共文件类型相关联(使用“打开方式”上下文菜单)的方法是使用文件类型键下的OpenWithProgIds 注册表键。
    【解决方案3】:

    此外,如果您决定采用注册表方式,请记住当前用户关联位于 HKEY_CURRENT_USER\Software\Classes 下。将您的应用程序添加到那里而不是本地机器类可能会更好。

    如果您的程序将由有限的用户运行,您将无法修改 CLASSES_ROOT。

    【讨论】:

      【解决方案4】:

      您可能出于特定原因选择不为您的项目使用安装包,但安装包是轻松执行应用程序配置任务(例如注册文件扩展名、添加桌面快捷方式等)的好地方。

      以下是使用内置 Visual Studio 安装工具创建文件扩展名关联的方法:

      1. 在您现有的 C# 解决方案中,添加一个新项目并选择项目类型为 Other Project Types -> Setup and Deployment -> Setup Project(或尝试安装向导)

      2. 配置您的安装程序(如果您需要帮助,可以使用大量现有文档)

      3. 右键单击解决方案资源管理器中的安装项目,选择View -> File Types,然后添加您要注册的扩展程序以及运行它的程序。

      如果用户为您的应用程序运行卸载,此方法还有一个额外的好处,即自行清理。

      【讨论】:

        【解决方案5】:

        如果您使用 ClickOnce 部署,这一切都会为您处理(至少,在 VS2008 SP1 中);简单地说:

        • 项目属性
        • 发布
        • 选项
        • 文件关联
        • (添加您需要的任何内容)

        (请注意,它必须是完全信任的,面向 .NET 3.5,并设置为离线使用)

        另见 MSDN:How to: Create File Associations For a ClickOnce Application

        【讨论】:

        • 我有 VS2008 Professional,SP1,但我没有看到这个选项。我一定是错过了什么。
        【解决方案6】:

        具体说一下“Windows 注册表”的方式:

        我在 HKEY_CURRENT_USER\Software\Classes 下创建密钥(就像 Ishmaeel 所说)

        并按照 X-Cubed 回答的说明进行操作。

        示例代码如下:

        private void Create_abc_FileAssociation()
        {
            /***********************************/
            /**** Key1: Create ".abc" entry ****/
            /***********************************/
            Microsoft.Win32.RegistryKey key1 = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", true);
        
            key1.CreateSubKey("Classes");
            key1 = key1.OpenSubKey("Classes", true);
        
            key1.CreateSubKey(".abc");
            key1 = key1.OpenSubKey(".abc", true);
            key1.SetValue("", "DemoKeyValue"); // Set default key value
        
            key1.Close();
        
            /*******************************************************/
            /**** Key2: Create "DemoKeyValue\DefaultIcon" entry ****/
            /*******************************************************/
            Microsoft.Win32.RegistryKey key2 = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", true);
        
            key2.CreateSubKey("Classes");
            key2 = key2.OpenSubKey("Classes", true);
        
            key2.CreateSubKey("DemoKeyValue");
            key2 = key2.OpenSubKey("DemoKeyValue", true);
        
            key2.CreateSubKey("DefaultIcon");
            key2 = key2.OpenSubKey("DefaultIcon", true);
            key2.SetValue("", "\"" + "(The icon path you desire)" + "\""); // Set default key value
        
            key2.Close();
        
            /**************************************************************/
            /**** Key3: Create "DemoKeyValue\shell\open\command" entry ****/
            /**************************************************************/
            Microsoft.Win32.RegistryKey key3 = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", true);
        
            key3.CreateSubKey("Classes");
            key3 = key3.OpenSubKey("Classes", true);
        
            key3.CreateSubKey("DemoKeyValue");
            key3 = key3.OpenSubKey("DemoKeyValue", true);
        
            key3.CreateSubKey("shell");
            key3 = key3.OpenSubKey("shell", true);
        
            key3.CreateSubKey("open");
            key3 = key3.OpenSubKey("open", true);
        
            key3.CreateSubKey("command");
            key3 = key3.OpenSubKey("command", true);
            key3.SetValue("", "\"" + "(The application path you desire)" + "\"" + " \"%1\""); // Set default key value
        
            key3.Close();
        }
        

        只是给你们看一个简单的演示,非常容易理解。您可以修改这些键值,一切顺利。

        【讨论】:

        • 不要忘记添加必要的代码来通知外壳使用图标。 SHChangeNotify(0x8000000, 0, 0, 0);
        【解决方案7】:

        这是一个完整的例子:

        public class FileAssociation
        {
            public string Extension { get; set; }
            public string ProgId { get; set; }
            public string FileTypeDescription { get; set; }
            public string ExecutableFilePath { get; set; }
        }
        
        public class FileAssociations
        {
            // needed so that Explorer windows get refreshed after the registry is updated
            [System.Runtime.InteropServices.DllImport("Shell32.dll")]
            private static extern int SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2);
        
            private const int SHCNE_ASSOCCHANGED = 0x8000000;
            private const int SHCNF_FLUSH = 0x1000;
        
            public static void EnsureAssociationsSet()
            {
                var filePath = Process.GetCurrentProcess().MainModule.FileName;
                EnsureAssociationsSet(
                    new FileAssociation
                    {
                        Extension = ".binlog",
                        ProgId = "MSBuildBinaryLog",
                        FileTypeDescription = "MSBuild Binary Log",
                        ExecutableFilePath = filePath
                    },
                    new FileAssociation
                    {
                        Extension = ".buildlog",
                        ProgId = "MSBuildStructuredLog",
                        FileTypeDescription = "MSBuild Structured Log",
                        ExecutableFilePath = filePath
                    });
            }
        
            public static void EnsureAssociationsSet(params FileAssociation[] associations)
            {
                bool madeChanges = false;
                foreach (var association in associations)
                {
                    madeChanges |= SetAssociation(
                        association.Extension,
                        association.ProgId,
                        association.FileTypeDescription,
                        association.ExecutableFilePath);
                }
        
                if (madeChanges)
                {
                    SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_FLUSH, IntPtr.Zero, IntPtr.Zero);
                }
            }
        
            public static bool SetAssociation(string extension, string progId, string fileTypeDescription, string applicationFilePath)
            {
                bool madeChanges = false;
                madeChanges |= SetKeyDefaultValue(@"Software\Classes\" + extension, progId);
                madeChanges |= SetKeyDefaultValue(@"Software\Classes\" + progId, fileTypeDescription);
                madeChanges |= SetKeyDefaultValue($@"Software\Classes\{progId}\shell\open\command", "\"" + applicationFilePath + "\" \"%1\"");
                return madeChanges;
            }
        
            private static bool SetKeyDefaultValue(string keyPath, string value)
            {
                using (var key = Registry.CurrentUser.CreateSubKey(keyPath))
                {
                    if (key.GetValue(null) as string != value)
                    {
                        key.SetValue(null, value);
                        return true;
                    }
                }
        
                return false;
            }
        

        【讨论】:

          【解决方案8】:

          自 Windows 7 以来就有两个 cmd 工具,可以很容易地创建简单的文件关联。他们是assoc 和ftype。这是每个命令的基本解释。

          • Assoc - 将文件扩展名(如“.txt”)与“文件类型”相关联。
          • FType - 定义在用户打开给定“文件类型”时运行的可执行文件。

          请注意,这些是 cmd 工具,而不是可执行文件 (exe)。这意味着它们只能在 cmd 窗口中运行,或者通过使用带有“cmd /c assoc”的 ShellExecute 来运行。您可以通过链接或键入“assoc /?”了解更多关于它们的信息。和“ftype /?”在 cmd 提示符下。

          因此,要将应用程序与 .bob 扩展名关联,您可以打开一个 cmd 窗口(WindowKey+R,键入 cmd,按 Enter)并运行以下命令:

          assoc .bob=BobFile
          ftype BobFile=c:\temp\BobView.exe "%1"
          

          这比弄乱注册表要简单得多,而且更有可能在未来的 Windows 版本中工作。

          总结一下,这是一个创建文件关联的 C# 函数:

          public static int setFileAssociation(string[] extensions, string fileType, string openCommandString) {
              int v = execute("cmd", "/c ftype " + fileType + "=" + openCommandString);
              foreach (string ext in extensions) {
                  v = execute("cmd", "/c assoc " + ext + "=" + fileType);
                  if (v != 0) return v;
              }
              return v;
          }
          public static int execute(string exeFilename, string arguments) {
              ProcessStartInfo startInfo = new ProcessStartInfo();
              startInfo.CreateNoWindow = false;
              startInfo.UseShellExecute = true;
              startInfo.FileName = exeFilename;
              startInfo.WindowStyle = ProcessWindowStyle.Hidden;
              startInfo.Arguments = arguments;
              try {
                  using (Process exeProcess = Process.Start(startInfo)) {
                      exeProcess.WaitForExit();
                      return exeProcess.ExitCode;
                  }
              } catch {
                  return 1;
              }
          }
          

          【讨论】:

            【解决方案9】:

            下面的代码是一个应该可以工作的函数,它在 Windows 注册表中添加了所需的值。通常我在我的可执行文件中运行 SelfCreateAssociation(".abc") 。 (表单构造函数或 onload 或 onshown)每次执行可执行文件时,它将更新当前用户的注册表项。 (适合调试,如果您有一些更改)。 如果您需要有关所涉及的注册表项的详细信息,请查看此 MSDN 链接。

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

            获取有关常规 ClassesRoot 注册表项的更多信息。请参阅这篇 MSDN 文章。

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

            public enum KeyHiveSmall
            {
                ClassesRoot,
                CurrentUser,
                LocalMachine,
            }
            
            /// <summary>
            /// 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");
            /// </summary>
            /// <param name="ProgID">e.g. vendor.application</param>
            /// <param name="extension">e.g. .tmf</param>
            /// <param name="description">e.g. Tool file</param>
            /// <param name="application">e.g.  @"C:\Windows\SYSWOW64\notepad.exe"</param>
            /// <param name="icon">@"%SystemRoot%\SYSWOW64\notepad.exe,0"</param>
            /// <param name="hive">e.g. The user-specific settings have priority over the computer settings. KeyHive.LocalMachine  need admin rights</param>
            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();
            }
            
             /// <summary>
                /// Creates a association for current running executable
                /// </summary>
                /// <param name="extension">e.g. .tmf</param>
                /// <param name="hive">e.g. KeyHive.LocalMachine need admin rights</param>
                /// <param name="description">e.g. Tool file. Displayed in explorer</param>
                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);
                }
            

            【讨论】:

            • 留下一大块没有解释的代码对未来的读者几乎没有帮助。请尝试添加一些关于此代码的作用的解释。
            • 代码正在运行。源代码中的解释很好,对我来说很完美。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-11-02
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多