处理由Process.Start()引发的Win32Exception [英] Handle Win32Exception thrown by Process.Start()

查看:75
本文介绍了处理由Process.Start()引发的Win32Exception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些计算机上,当我调用 Process.Start()以启动我的助手可执行文件时,出现以下异常:

On some computers, when I call Process.Start() to start my helper executable, I get the following exception:

System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()

问题:当我得到 Win32Exception 时,我想知道这是我描述的找不到文件"异常还是其他原因.如何可靠地做到这一点?我应该将此 0x80004005 与某些内容进行比较,还是应该分析错误消息?

The question: when I get Win32Exception, I want to tell if this is the "file not found" exception I described or something else. How to do this reliably? Should I compare this 0x80004005 against something, or should I parse the error message?

一些解释:可执行文件可能由于某种原因而被删除(这就是为什么找不到文件的原因).这是预期的情况.如果由于其他原因引发了异常,我想报告它.

Some explanation: the executable may get removed by for some reason (that's why file not found). This is an expected situation. If the exception was thrown with some other reason, I want to report it.

推荐答案

我认为应该这样做:

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
    }
}

E_FAIL是 HRESULT值,ERROR_FILE_NOT_FOUND是系统错误代码.

E_FAIL is a HRESULT value, ERROR_FILE_NOT_FOUND is a system error code.

不应使用该错误消息,因为它取决于区域性,并且实际上是系统错误代码的翻译.

The error message shouldn't be used because it is culture-dependent and is actually a translation of the system error code.

这篇关于处理由Process.Start()引发的Win32Exception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆