编程语言
首页 > 编程语言> > c# – 为什么Process.Start(string)为WMV文件返回null

c# – 为什么Process.Start(string)为WMV文件返回null

作者:互联网

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

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

我正在启动视频并使用代码获取我的Process ID:

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

虽然我的视频文件启动,但进程始终为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文件”.在您的方案中,您依赖OS文件扩展名处理程序映射来调用默认应用程序来处理它.

UPDATE

来自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,它始终是up.

更新2

下面是使用Process.Start开始播放WMV文件后从SysInternals截取的屏幕截图:

正如您所看到的,wmplayer在svchost.exe的控制下打开,因此在您请求WMV文件时,svchost已经启动,因此Start,根据设计返回null. PPT,或者更确切地说是PowerPoint,将在一个单独的过程中打开,而不是在svchost的控制下.

标签:c,process,wmv
来源: https://codeday.me/bug/20190628/1317022.html