我提供文件從我的網站下載到用戶。當文件存在時,它工作正常。但是,如果該文件是無論出於何種原因刪除,我得到以下錯誤在Visual Studio:處理DirectoryNotFoundException錯誤
An exception of type 'System.IO.DirectoryNotFoundException' occurred in
mscorlib.dll but was not handled in user code
和用戶只是看到網站上的JSON字符串。
我用這個報價上漲流:
var result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(
new FileStream(mediaFile.FilesystemLocation, FileMode.Open));
mediaFile.FilesystemLocation
很簡單:
public virtual string FilesystemLocation
{
get { return Path.Combine(FilesystemRoot, Id + "." + Extension); }
}
我試圖把整個事情在try/catch塊,但隨後失去了所有它的引用到其他班級。
所以我的問題是,我如何處理這段代碼並防止這個錯誤?
理想情況下,我只想向用戶顯示一條消息,「找不到文件,請聯繫您的管理員」或類似的東西。
謝謝!
'把整個事情放在try/catch塊'不應該導致任何引用丟失。 – 2014-12-04 17:33:08