C#文件类型关联

本文关键字:关联 类型 文件 | 更新日期: 2023-09-27 17:58:07

对于给定的文件类型,如何以编程方式将应用程序添加到"打开方式"菜单?

例如,我做了一个简单的文本文件查看器,我也做了同样的设置项目,我想将文本文件查看器与系统中的所有.txt文件相关联,当用户双击任何.txt文件时,我的应用程序就会打开。

使用菜单打开http://i4.photoblog.com/photos/27294-1306838510-0.jpg

C#文件类型关联

您可以使用这个伟大的类来实现这一点:http://www.mentalis.org/soft/class.qpx?id=5

您必须更改注册表项:HKEY_CLASSES_ROOT'txtfile'shell'open'command。看看regedit.exe。你也可以看看这个密钥:HKEY_CLASSES_ROOT'.txt

要操作注册表项,请将System.Win32.Registry与此处的文档一起使用。

这里有一种方法(在VB中)。ApplicationTag是注册表的短名称,例如editor3.1。您可以使用regedit检查注册表以查看发生了什么,并且您可能需要在测试应用程序的这一部分之前创建一个恢复点。

Imports Microsoft.Win32

Registry.SetValue("HKEY_CURRENT_USER'software'classes'" & FileType, "", applicationTag)
q = Registry.CurrentUser.OpenSubKey("Software'Microsoft'Windows'CurrentVersion'Explorer'FileExts'" & FileType, True)
If q IsNot Nothing AndAlso q.GetValue("ProgID", "notfound") <> "notfound" Then
  q.SetValue("ProgID", appTag) ' for the local user, overrides hkcr
  End If
appKey = "HKEY_CURRENT_USER'software'classes'" & applicationTag
Registry.SetValue(appKey, "", "text")
Registry.SetValue(appKey & "'shell", "", "open")
Registry.SetValue(appKey & "'shell'open", "", "")
Registry.SetValue(appKey & "'shell'open'command", "", """" & ApplicationPath & """ ""%1""")
Registry.SetValue(appKey, "", "text")
appKey = "HKEY_CURRENT_USER'software'classes'CLSID'" & ApplicationGuid
Registry.SetValue(appKey, "", applicationTitle)
Registry.SetValue(appKey & "'ProgID", "", applicationTag)