C#文件types关联

我该如何向给定文件types的Open With菜单中添加应用程序?

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

打开菜单http://img.zgserver.com/c%23/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)