[英]What is the common way to throw exception if my exe file that i need to start does not exist on the machine
在我的應用程序中,我啟動Wireshark的capinfos.exe。 在構造函數中,我正在檢查是否在計算機上安裝了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
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/zh-cn/library/0yd65esw(v=vs.80).aspx
就像是:
throw new FileNotFoundException("Could not find " + _capInfos, _capInfos);
不知道這是否是您想要的,但是嘗試使用try-catch塊。 您可以嘗試在try塊中啟動.exe,如果失敗,則拋出FileNotFoundException並在catch塊中創建一個彈出框,該框將提醒用戶他們需要做什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.