Bar*_*ead 52 .net c# windows file-type
我正在使用C#开发一个面向.NET Framework 2.0的应用程序,我需要能够找到用于打开特定文件类型的默认应用程序.
我知道,例如,如果您只想使用该应用程序打开文件,您可以使用以下内容:
System.Diagnostics.Process.Start( "C:\...\...\myfile.html" );
Run Code Online (Sandbox Code Playgroud)
在默认浏览器中打开HTML文档,或
System.Diagnostics.Process.Start( "C:\...\...\myfile.txt" );
Run Code Online (Sandbox Code Playgroud)
在默认文本编辑器中打开文本文件.
但是,我希望能够在默认文本编辑器中打开不一定具有.txt扩展名的文件(例如),因此我需要能够找到打开的默认应用程序. txt文件,这将允许我直接调用它.
我猜测有一些Win32 API,我需要P/Invoke才能做到这一点,但是对谷歌和MSDN的快速浏览并没有发现任何有趣的东西; 我确实找到了大量完全不相关的页面,但没有像我在寻找的那样.
Oha*_*der 68
目前所有答案都不可靠.注册表是一个实现细节,实际上这些代码在我的Windows 8.1机器上被破坏了.正确的方法是使用Win32 API,特别是AssocQueryString:
using System.Runtime.InteropServices;
[DllImport("Shlwapi.dll", CharSet = CharSet.Unicode)]
public static extern uint AssocQueryString(
AssocF flags,
AssocStr str,
string pszAssoc,
string pszExtra,
[Out] StringBuilder pszOut,
ref uint pcchOut
);
[Flags]
public enum AssocF
{
None = 0,
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,
Init_IgnoreUnknown = 0x400,
Init_Fixed_ProgId = 0x800,
Is_Protocol = 0x1000,
Init_For_File = 0x2000
}
public enum AssocStr
{
Command = 1,
Executable,
FriendlyDocName,
FriendlyAppName,
NoOpen,
ShellNewValue,
DDECommand,
DDEIfExec,
DDEApplication,
DDETopic,
InfoTip,
QuickTip,
TileInfo,
ContentType,
DefaultIcon,
ShellExtension,
DropTarget,
DelegateExecute,
Supported_Uri_Protocols,
ProgID,
AppID,
AppPublisher,
AppIconReference,
Max
}
Run Code Online (Sandbox Code Playgroud)
相关文件:
样品用法:
static string AssocQueryString(AssocStr association, string extension)
{
const int S_OK = 0;
const int S_FALSE = 1;
uint length = 0;
uint ret = AssocQueryString(AssocF.None, association, extension, null, null, ref length);
if (ret != S_FALSE)
{
throw new InvalidOperationException("Could not determine associated string");
}
var sb = new StringBuilder((int)length); // (length-1) will probably work too as the marshaller adds null termination
ret = AssocQueryString(AssocF.None, association, extension, null, sb, ref length);
if (ret != S_OK)
{
throw new InvalidOperationException("Could not determine associated string");
}
return sb.ToString();
}
Run Code Online (Sandbox Code Playgroud)
卫生署!当然.
HKEY_CLASSES_ROOT\.txt
Run Code Online (Sandbox Code Playgroud)
包括对...的引用
HKEY_CLASSES_ROOT\txtfile
Run Code Online (Sandbox Code Playgroud)
其中包含一个子项
HKEY_CLASSES_ROOT\txtfile\shell\open\command
Run Code Online (Sandbox Code Playgroud)
其中引用了记事本.
排序,非常感谢!
巴特
这是一篇关于此主题的博客文章.代码示例在VB.net中,但是将它们移植到C#应该很容易.
| 归档时间: |
|
| 查看次数: |
42689 次 |
| 最近记录: |