我在嘗試使用try-catch塊執行函數時遇到FileNotFoundException。我嘗試過捕獲FileNotFoundException,無濟於事。誰能告訴我爲什麼這樣做?拋出FileNotFoundException但未捕獲
public static bool IsKeyValid(string path)
{
bool rVal = false;
try
{
Stream stream = File.Open(path + "\\data.bin", FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
ValidKey vk = (ValidKey)bf.Deserialize(stream);
if (vk.SerialNumber != null)
rVal = true;
else
rVal = false;
}
catch (Exception fnfe)
{
rVal = false;
}
return rVal;
}
你有沒有考慮檢查文件是否真的存在? – dtb
你在路徑變量中傳遞了什麼? –
首先:仔細檢查你的路徑變量。第二:使用Path.Combine方法。 – Dariusz