2012-04-09 142 views
0

我有一個奇怪的問題。 我將上傳的文件保存到數據庫,然後嘗試從上傳文件夾中刪除上傳的文件 。File.Delete()不能在運行模式下工作,但只能在調試模式下工作

這在調試模式下工作正常,但在運行模式下,文件保持未刪除狀態。

任何人都遇到過這個問題?

這是.NET低於4

代碼片段:

private string SaveFiles(string rootFolder) 
{ 
    var uploadedPhotos = GetAllFilesUploaded(); 
foreach (var file in uploadedFiles) 
{ 
       string path= Path.Combine(rootFolder, "userfile", file.FileName); 

       FileService.SaveUploadedFile(fileName, GetBytesFromLocalFile(path)); 

       File.Delete(path); <-- this only works in debug mode!! 

       } 
    } 

    public static byte[] GetBytesFromLocalFile(string filePath) 
      { 
       using (FileStream fs = new FileStream(filePath, FileMode.Open)) 
       { 
        byte[] bytes = new byte[fs.Length]; 
        fs.Read(bytes, 0, (int)fs.Length); 
        return bytes; 
       } 
      } 
+0

我遇到同樣的問題。你有沒有解決這個問題? – Jelling 2014-03-20 22:26:10

回答

2

IMO,因爲它是在調試模式下工作,那麼這是沒有編碼問題。問題在於你爲File.Delete(路徑)提供的路徑。因爲根據http://msdn.microsoft.com/en-us/library/system.io.file.delete.aspx

If the file to be deleted does not exist, no exception is thrown. 

在發佈模式下檢查路徑。可能與它在bin文件夾中的發佈和調試文件夾有關。

2

要添加到Nikhil的答案,我會建議在釋放模式下將MessageBoxpath並手動檢查路徑是否正確。

注意:不要忘記刪除後MessageBox

+0

從我+1爲您的答案 – 2012-04-09 03:51:52

相關問題