为什么 Process.Start(string) return 对于 WMV 文件为 null

Why does Process.Start(string) return null for a WMV file

所以我有一个 WMV 视频文件:

var fileName = @"C:\MyFolder\MyVideo.WMV"

我开始播放视频并使用以下代码获取我的进程 ID:

var process = Process.Start(fileName);
if (process != null)
{
    processId = process.Id;
}

虽然我的视频文件开始了,process总是空的。

来自 Process.Start(string) MSDN 我可以看到:

Return Value Type:

System.Diagnostics.Process

A new Process that is associated with the process resource, or null if no process resource is started. Note that a new process that’s started alongside already running instances of the same process will be independent from the others. In addition, Start may return a non-null Process with its ProcessHasExited property already set to true. In this case, the started process may have activated an existing instance of itself and then exited.

说如果没有启动新进程则返回null。但是我的进程已经启动并且 null 仍然返回。这是为什么?

因为你不能"start a WMV file"。在您的场景中,您依赖 OS 文件扩展名处理程序映射来调用默认应用程序来处理它。

更新

来自 MSDN 文档:

Use this overload to start a process resource by specifying its file name. The overload associates the resource with a new Process component. If the process is already running, no additional process resource is started. Instead, the existing process resource is reused and no new Process component is created. In such a case, instead of returning a new Process component, Start returns null to the calling procedure.

是否有可能某些 OS 负责将您的媒体内容请求定向到已注册应用程序以进行扩展的小发明已经 运行?我会说很可能,从逻辑上讲它会是 explorer.exe,它总是在运行。

更新 2

这是使用 Process.Start 开始播放 WMV 文件后来自 SysInternals 的屏幕截图:

如您所见,wmplayer 在 svchost.exe 的控制下打开,因此在您请求 WMV 文件时,svchost 已经启动,因此启动,returns null 按照设计。 PPT,或者说 PowerPoint,将在一个单独的进程中打开,不受 svchost 的控制。