获取txt文件的标准应用程序(.NET)

可能重复:
find在Windows上打开特定文件types的默认应用程序

你好,在我的应用程序中,我想打开一个没有扩展名为.txt的文本文件。 有没有什么办法可以在.NET(C#)中获得.txt文件的标准应用程序? 当然我可以用“记事本”,但也许会有一些人(比如我)喜欢另一个(他们的标准)编辑。

非常感谢你。


编辑:

registry项“[HKEY_CLASSES_ROOT] \ txtfile \ shell \ open \ command”引用记事本,但这不是我的txt文件的标准应用程序。 如何获得我当前的.txt标准应用程序?

绝对最好的选择是使用ShellExecuteEx传递SEE_MASK_CLASSNAME标志来打开这个文件,就好像它是一个".txt" 。 这是唯一可以支持诸如基于DDE和基于拖放的文件打开机制的方法。

但是,如果你想自己做(例如运行部分信任,不能p /调用ShellExecuteEx ),这是如何:

还有另一个层次的间接。 你首先要查找

HKCR \ .TXT

从该键读取默认值,将其称为txtkey

然后检查

HKCR \ txtkey \ shell \ open \命令

还有一个函数可以为你做这件事: AssocQueryString

据我所知你必须通过P / Invoke访问它,但最简洁的方法可能是创建一个临时文件与您关心的扩展名,并调用FindExecutable临时文件。 然后,您可以使用该可执行文件打开您关心的文件。

我的代码,包括一些检查,以防止一些常见的错误…希望它有助于:-)

 using System; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using System.Text; namespace HQ.Util.Unmanaged { /// <summary> /// Usage: string executablePath = FileAssociation.GetExecFileAssociatedToExtension(pathExtension, "open"); /// </summary> public static class FileAssociation { /// <summary> /// /// </summary> /// <param name="ext"></param> /// <param name="verb"></param> /// <returns>Return null if not found</returns> public static string GetExecFileAssociatedToExtension(string ext, string verb = null) { if (ext[0] != '.') { ext = "." + ext; } string executablePath = FileExtentionInfo(AssocStr.Executable, ext, verb); // Will only work for 'open' verb if (string.IsNullOrEmpty(executablePath)) { executablePath = FileExtentionInfo(AssocStr.Command, ext, verb); // required to find command of any other verb than 'open' // Extract only the path if (!string.IsNullOrEmpty(executablePath) && executablePath.Length > 1) { if (executablePath[0] == '"') { executablePath = executablePath.Split('\"')[1]; } else if (executablePath[0] == '\'') { executablePath = executablePath.Split('\'')[1]; } } } // Ensure to not return the default OpenWith.exe associated executable in Windows 8 or higher if (!string.IsNullOrEmpty(executablePath) && File.Exists(executablePath) && !executablePath.ToLower().EndsWith(".dll")) { if (executablePath.ToLower().EndsWith("openwith.exe")) { return null; // 'OpenWith.exe' is th windows 8 or higher default for unknown extensions. I don't want to have it as associted file } return executablePath; } return executablePath; } [DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern uint AssocQueryString(AssocF flags, AssocStr str, string pszAssoc, string pszExtra, [Out] StringBuilder pszOut, [In][Out] ref uint pcchOut); private static string FileExtentionInfo(AssocStr assocStr, string doctype, string verb) { uint pcchOut = 0; AssocQueryString(AssocF.Verify, assocStr, doctype, verb, null, ref pcchOut); Debug.Assert(pcchOut != 0); if (pcchOut == 0) { return ""; } StringBuilder pszOut = new StringBuilder((int)pcchOut); AssocQueryString(AssocF.Verify, assocStr, doctype, verb, pszOut, ref pcchOut); return pszOut.ToString(); } [Flags] public enum AssocF { Init_NoRemapCLSID = 0x1, Init_ByExeName = 0x2, Open_ByExeName = 0x2, Init_DefaultToStar = 0x4, Init_DefaultToFolder = 0x8, NoUserSettings = 0x10, NoTruncate = 0x20, Verify = 0x40, RemapRunDll = 0x80, NoFixUps = 0x100, IgnoreBaseClass = 0x200 } public enum AssocStr { Command = 1, Executable, FriendlyDocName, FriendlyAppName, NoOpen, ShellNewValue, DDECommand, DDEIfExec, DDEApplication, DDETopic } } } 

试着直接打开.txt文件。

 Process.Start("MyFile.txt"); 

它应该使用首选的文本编辑器自动打开。

编辑:

哎呀,没有看到它没有.txt扩展名。 可能必须检查注册表中的首选编辑器?

编辑:

这是在我的电脑(Windows 7)上:HKEY_CLASSES_ROOT \ textfile \ shell \ open \ command