2013-10-26 38 views
0

我無法刪除與此命令文件中刪除文件..不能與File.Delete(路徑)

Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 

即時得到上述錯誤.. 但是林能夠下載文件與同樣的路徑..

其他細節:我已經有文件路徑保存在數據庫中.. 和文件都在〜\上傳\文件夾..

回答

1

這樣的路徑「〜\ Upload \ folder」是虛擬路徑。您需要將它們轉換爲物理路徑才能刪除它們。

如果您在web上下文中,則使用以下代碼獲取物理路徑,然後刪除它們。

var physicalPath = HttpContext.Current.Server.MapPath("~/Upload/folder/file.html"); //to get the physical path 

File.Delete(physicalPath); 
+0

非常感謝主席先生。我能下載的文件與舊的路徑,所以我試圖刪除具有相同路徑太..感謝您的幫助 – Hus

+0

@Hus歡迎您 –

0

確保您逃避任何反斜槓/着,並且只是通常檢查你的路徑是否完整,沒有sma會發生錯誤。

失敗,確保您的程序具有刪除該文件的正確權限。

對不起,我不能更具體,但你沒有顯示你的代碼。