首页 > 解决方案 > 如何确保 Process.Start 不会抛出 Win32Exception "File not found"

问题描述

我通过引用仅文件名的可执行文件生成 ProcessStartInfo,而不提供绝对路径。我希望操作系统通过使用 PATH 环境变量来解析具体位置。

但是,在某些安装中,PATH 变量设置不正确,并且在通过调用启动进程Process.Start()时会抛出一个

System.ComponentModel.Win32Exception

该系统找不到指定的文件

是否有编程选项来检查 Process.Start 是否可以预先工作?

我尝试使用File.Exists(fileName),但这仅适用于位于应用程序工作目录中的文件。

在命令提示符下,您可以使用以下调用来确定它是否有效:

//example where a file is found
C:\>where calc.exe 
C:\Windows\System32\calc.exe

//example where a file is not found
C:\>where foo.exe
INFO: Could not find files for the given pattern(s).

.net 中是否有类似的东西可以预先确定文件路径是否可以解析,以便 Process.Start() 不会抛出?

标签: c#processsystem.diagnostics

解决方案


推荐阅读