首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果我需要启动的exe文件在机器上不存在,抛出异常的常见方法是什么?

如果我需要启动的exe文件在机器上不存在,抛出异常的常见方法是什么?
EN

Stack Overflow用户
提问于 2012-12-06 02:14:12
回答 3查看 397关注 0票数 0

在我的应用程序中,我启动了capinfos.exe,它是Wireshark的一部分。在构造函数中,我正在检查机器上是否安装了Wireshark:

代码语言:javascript
复制
private string _filePath = "";

public Capinfos(string capturePath)
{
    if (Directory.Exists(@"C:\Program Files (x86)\Wireshark"))
    {
        _capInfos = @"C:\Program Files (x86)\Wireshark\capinfos.exe";
    }
    else if (Directory.Exists(@"C:\Program Files\Wireshark"))
    {
        _capInfos = @"C:\Program Files\Wireshark\capinfos.exe";
    }

    _filePath = capturePath;
}

如果计算机上不存在该文件,那么执行此操作并抛出异常的最佳方法是什么:请安装Wireshark

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-12-06 02:26:05

代码语言:javascript
复制
private string _filePath = "";

public Capinfos(string capturePath) throws FileNotFoundException
{
    if (Directory.Exists(@"C:\Program Files (x86)\Wireshark"))
    {
        _capInfos = @"C:\Program Files (x86)\Wireshark\capinfos.exe";
    }
    else if (Directory.Exists(@"C:\Program Files\Wireshark"))
    {
        _capInfos = @"C:\Program Files\Wireshark\capinfos.exe";
    } else
    {
       throw new FileNotFoundException(@"Wireshark installation not found");
    } 

    _filePath = capturePath;
}

然后,您可以使用以下代码捕获异常:

代码语言:javascript
复制
   try
    {
        Capinfos("path");
    }
    catch (FileNotFoundException ex)
    {
        Messagebox.Show("Please install wireshark.");
    }

我没有安装C#,这是手写的。希望它是好的!这里有一个很好的学习异常的资源:http://msdn.microsoft.com/en-us/library/0yd65esw(v=vs.80).aspx

票数 1
EN

Stack Overflow用户

发布于 2012-12-06 02:23:57

类似于:

代码语言:javascript
复制
throw new FileNotFoundException("Could not find " + _capInfos, _capInfos);
票数 0
EN

Stack Overflow用户

发布于 2012-12-06 02:25:19

不确定这是否是您想要的,但请尝试使用try-catch块。您可以尝试在try块中启动.exe,如果失败,则抛出一个FileNotFoundException并在catch块中创建一个弹出框来提醒用户他们需要做什么。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13729727

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档