C# 如何使用 shell 的默认处理程序打开文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/811521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I open a file using the shell's default handler?
提问by Simon
Our client (a winforms app) includes a file-browser. I'd like the user to be able to open the selected file using the shell's default handler. How do I do that? I've read that I should use the Win32 API rather than the registry, but I'd prefer a solution that involves only .NET.
我们的客户端(一个 winforms 应用程序)包括一个文件浏览器。我希望用户能够使用 shell 的默认处理程序打开选定的文件。我怎么做?我读过我应该使用 Win32 API 而不是注册表,但我更喜欢只涉及 .NET 的解决方案。
采纳答案by Jon Skeet
EDIT: Newer, simpler answer.
编辑:更新,更简单的答案。
You can indeed just use Process.Start(filename)
. This is specified in the docs for Process.Start
:
你确实可以只使用Process.Start(filename)
. 这是在文档中Process.Start
指定的:
Starting a process by specifying its file name is similar to typing the information in the Run dialog box of the Windows Start menu. Therefore, the file name does not need to represent an executable file. It can be of any file type for which the extension has been associated with an application installed on the system. For example the file name can have a .txt extension if you have associated text files with an editor, such as Notepad, or it can have a .doc if you have associated.doc files with a word processing tool, such as Microsoft Word. Similarly, in the same way that the Run dialog box can accept an executable file name with or without the .exe extension, the .exe extension is optional in the fileName parameter. For example, you can set the fileName parameter to either "Notepad.exe" or "Notepad".
通过指定文件名启动进程类似于在 Windows 开始菜单的运行对话框中键入信息。因此,文件名不需要代表可执行文件。它可以是扩展名与系统上安装的应用程序相关联的任何文件类型。例如,如果您将文本文件与记事本等编辑器相关联,则文件名可以具有 .txt 扩展名;如果您将 .doc 文件与字处理工具(如 Microsoft Word)相关联,则文件名可以具有 .doc。同样,与“运行”对话框可以接受带有或不带有 .exe 扩展名的可执行文件名一样,.exe 扩展名在 fileName 参数中是可选的。例如,您可以将 fileName 参数设置为“Notepad.exe”
EDIT: Original, complicated answer:
编辑:原始的,复杂的答案:
If you use Process.Start
with the file as the "executable" and specify UseShellExecute = true
it will just work. For example:
如果您Process.Start
将该文件用作“可执行文件”并指定UseShellExecute = true
它就可以工作。例如:
using System;
using System.Diagnostics;
class Test
{
static void Main()
{
ProcessStartInfo psi = new ProcessStartInfo("test.txt");
psi.UseShellExecute = true;
Process.Start(psi);
}
}
That opens test.txt in Notepad.
这会在记事本中打开 test.txt。
In fact, UseShellExecute=true
is the default, but as it's definitely required I like to specify it explicitly to make that clearer to the reader.
事实上,UseShellExecute=true
是默认值,但由于它绝对是必需的,我喜欢明确指定它以使读者更清楚。
回答by Jason
not sure if its ok
不确定是否可以
System.Diagnostics.Process.Start(filePath);
回答by Tetsujin no Oni
System.Diagnostics.Process provides the .Net native wrapper around shell32.ShellExecute.
System.Diagnostics.Process 提供了围绕 shell32.ShellExecute 的 .Net 本机包装器。
See PInvoke.Netfor a discussion of both APIs, and MSDN docs on MSDN.
请参阅PInvoke.Net以了解有关这两种 API 的讨论以及 MSDN 上的MSDN文档。