C# 使用关联的应用程序打开文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10174156/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:44:45  来源:igfitidea点击:

Open file with associated application

c#processfile-association

提问by Zavael

i want to ask for help with opening a file from c# app with associated app. I tried this:

我想寻求帮助从带有关联应用程序的 c# 应用程序打开文件。我试过这个:

      ProcessStartInfo pi = new ProcessStartInfo(file);
      pi.Arguments = Path.GetFileName(file);
      pi.UseShellExecute = true;
      pi.WorkingDirectory = Path.GetDirectoryName(file);
      pi.FileName = file;
      pi.Verb = "OPEN";
      Process.Start(pi);

or this:

或这个:

      Process.Start(file);

where string filein both examples represents full path to the file trying to open. Now, everything is working well, except the (jpg) images with ACDSee app. Irfanview associations works well, MS office documents too. After trying to open the jpg image associated with acdsee it just runs the acdsee in the notification area and does not open the file.

其中file两个示例中的 string表示尝试打开的文件的完整路径。现在,除了带有 ACDSee 应用程序(jpg) 图像外,一切都运行良好。Irfanview 协会运作良好,MS Office 文档也运作良好。在尝试打开与 acdsee 关联的 jpg 图像后,它只会在通知区域中运行 acdsee 而不会打开文件。

I discovered, that in the registry CLASSES_ROOTfor *.jpg images, there is an ACDSee.JPG value as associated app, and under this key there is in the shell->Open->Commanda path:

我发现,在*.jpg 图像的注册表CLASSES_ROOT中,有一个 ACDSee.JPG 值作为关联的应用程序,在此键下,shell- >Open->Command 中有一个路径:

"C:\Program Files\ACD Systems\ACDSee\ACDSee.exe" /dde

and I thing that this weird /ddeis the reason, why i cannot open the file. I realized that in the same reg key shell->Openthere is some DDEExeckey entry with value [open("%1")]

我认为这很奇怪/dde是我无法打开文件的原因。我意识到在同一个 reg key shell->Open 中有一些DDEExec带值的键条目[open("%1")]

For Irfan view or other checked app there is not a ddeexec, just the normal command like

对于 Irfan 视图或其他已检查的应用程序,没有 ddeexec,只是像这样的普通命令

"C:\Program Files (x86)\IrfanView\i_view32.exe" "%1"

that can be run from command line after swaping the %1 for file name, but I could not run the command from acdsee entry in the command line :(

可以在将 %1 交换为文件名后从命令行运行,但我无法从命令行中的 acdsee 条目运行该命令:(

So my question is, how can I set up the ProcessStartInfoobject to ensure that it will run all the filesas it would be in the explorer by doubleclick, the standards and this DDEExecones? Is there something other like DDEExecthat I shoul be aware of? thanks and sorry for my EN

所以我的问题是,我如何设置ProcessStartInfo对象以确保它通过双击、标准和这个来运行所有文件,就像它在资源管理器中DDEExec一样?还有其他类似的DDEExec事情我应该注意吗?感谢和对不起我的 EN

采纳答案by Tigran

Just write

写就好了

System.Diagnostics.Process.Start(@"file path");

example

例子

System.Diagnostics.Process.Start(@"C:\foo.jpg");
System.Diagnostics.Process.Start(@"C:\foo.doc");
System.Diagnostics.Process.Start(@"C:\foo.dxf");
...

And shell will run associated program reading it from the registry, like usual double click does.

shell 将运行相关程序从注册表中读取它,就像通常的双击一样。

回答by Tree

This is an old thread but just in case anyone comes across it like I did. pi.FileName needs to be set to the file name (and possibly full path to file ) of the executable you want to use to open your file. The below code works for me to open a video file with VLC.

这是一个旧线程,但以防万一有人像我一样遇到它。pi.FileName 需要设置为要用于打开文件的可执行文件的文件名(可能还有 file 的完整路径)。下面的代码适用于我用 VLC 打开视频文件。

var path = files[currentIndex].fileName;
var pi = new ProcessStartInfo(path)
{
    Arguments = Path.GetFileName(path),
    UseShellExecute = true,
    WorkingDirectory = Path.GetDirectoryName(path),
    FileName = "C:\Program Files (x86)\VideoLAN\VLC\vlc.exe",
    Verb = "OPEN"
};
Process.Start(pi)

Tigran's answer works but will use windows' default application to open your file, so using ProcessStartInfo may be useful if you want to open the file with an application that is not the default.

Tigran 的答案有效,但将使用 Windows 的默认应用程序打开您的文件,因此如果您想使用非默认应用程序打开文件,使用 ProcessStartInfo 可能会很有用。

回答by Mehdi Dehghani

In .Net Core(as of v2.2) it should be:

.Net Core(从 v2.2 开始)它应该是:

new Process
{
    StartInfo = new ProcessStartInfo(@"file path")
    {
        UseShellExecute = true
    }
}.Start();

Related github issue can be found here

可以在此处找到相关的 github 问题