在我的应用程序中,我启动了capinfos.exe,它是Wireshark的一部分。在构造函数中,我正在检查机器上是否安装了Wireshark:
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
发布于 2012-12-06 02:26:05
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;
}然后,您可以使用以下代码捕获异常:
try
{
Capinfos("path");
}
catch (FileNotFoundException ex)
{
Messagebox.Show("Please install wireshark.");
}我没有安装C#,这是手写的。希望它是好的!这里有一个很好的学习异常的资源:http://msdn.microsoft.com/en-us/library/0yd65esw(v=vs.80).aspx
发布于 2012-12-06 02:23:57
类似于:
throw new FileNotFoundException("Could not find " + _capInfos, _capInfos);发布于 2012-12-06 02:25:19
不确定这是否是您想要的,但请尝试使用try-catch块。您可以尝试在try块中启动.exe,如果失败,则抛出一个FileNotFoundException并在catch块中创建一个弹出框来提醒用户他们需要做什么。
https://stackoverflow.com/questions/13729727
复制相似问题