如何向给定文件类型的Open With菜单中添加应用程序?C#文件类型关联
例如我已经做了一个简单的文本文件查看器,我做了相同的安装项目, 我想将文本文件查看器与系统中的所有.txt文件相关联, 当用户双击任何.txt文件时,我的应用程序应该打开。
Open with menu http://i4.photoblog.com/photos/27294-1306838510-0.jpg
如何向给定文件类型的Open With菜单中添加应用程序?C#文件类型关联
例如我已经做了一个简单的文本文件查看器,我做了相同的安装项目, 我想将文本文件查看器与系统中的所有.txt文件相关联, 当用户双击任何.txt文件时,我的应用程序应该打开。
Open with menu http://i4.photoblog.com/photos/27294-1306838510-0.jpg
你可以使用这个伟大的阶级为: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
这里的Docs。
下面是关于如何做到这一点的一种方法(以VB)。 ApplicationTag是注册表的简称,如editor3.1
。您可以使用注册表检查注册表以查看发生了什么,并且在测试应用程序的这部分之前,您可能需要创建一个还原点。
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)