所以我有一个 WMV 视频文件:
var fileName = @"C:\MyFolder\MyVideo.WMV"
我开始播放视频并使用代码获取我的进程 ID:
var process = Process.Start(fileName);
if (process != null)
{
processId = process.Id;
}
虽然我的视频文件开始了,但是process
一直是null。
来自 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 仍然返回。这是为什么?
最佳答案
因为你不能“启动一个WMV文件”。在您的场景中,您依靠操作系统文件扩展名处理程序映射来调用默认应用程序来处理它。
更新
来自 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.
是否有可能某些负责将您的媒体内容请求定向到已注册应用程序以进行扩展的操作系统 gizmo 已经在运行?我会说很有可能,因为从逻辑上讲它应该是 explorer.exe,它始终处于运行状态。
更新 2
这是使用 Process.Start 开始播放 WMV 文件后来自 SysInternals 的屏幕截图:
如您所见,wmplayer 在 svchost.exe 的控制下打开,因此在您请求 WMV 文件时,svchost 已经启动,因此 Start 会按照设计返回 null。 PPT,或者更确切地说是 PowerPoint,将在一个单独的进程中打开,不受 svchost 的控制。
关于c# - 为什么 Process.Start(string) 为 WMV 文件返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30352088/