在某些计算机上,当我调用Process.Start()启动辅助程序可执行文件时,出现以下异常:
System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
Run Code Online (Sandbox Code Playgroud)
问题:当我得到时Win32Exception,我想知道这是我描述的“找不到文件”异常还是其他异常。如何可靠地做到这一点?我应该将其0x80004005与某些内容进行比较,还是应该解析错误消息?
一些解释:由于某种原因,可执行文件可能会被删除(这就是找不到文件的原因)。这是预期的情况。如果异常是由于其他原因引发的,我想报告它。
这就是我认为应该这样做的方式:
public static int E_FAIL = unchecked((int)0x80004005);
public static int ERROR_FILE_NOT_FOUND = 0x2;
// When the exception is caught
catch (Win32Exception e)
{
if(e.ErrorCode == E_FAIL && e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
// This is a "File not found" exception
}
else
{
// This is something else
}
}
Run Code Online (Sandbox Code Playgroud)
E_FAIL是一个HRESULT 值,ERROR_FILE_NOT_FOUND是一个系统错误代码。
不应使用错误消息,因为它与文化相关,并且实际上是系统错误代码的翻译。
| 归档时间: |
|
| 查看次数: |
1698 次 |
| 最近记录: |