2015-11-06 53 views
0

有人請幫我解決這個錯誤。該進程無法訪問文件'E:\NewFolder\img.jpg',因爲它正在被另一個進程使用。該進程無法訪問文件'E: NewFolder img.jpg',因爲它正在被另一個進程使用

  if (btn_Submit.Text == "Submit") 
       request.requestDetail.IMAGEPATH=Chosen_File==string.Empty ? Filepath +"NoImage.png" + ext : Filepath + txt_val.Text.ToUpper().Trim() + ext;   
      else 
       request.requestDetail.IMAGEPATH = Chosen_File == Filepath + "NoImage.png" ? (Filepath + "NoImage.png") : (Filepath + txt_val.Text.ToUpper().Trim() + ext); 

      ResponseDetail response = new ResponseDetail(); 
      ABC_BL bl = new ABC_BL(); 
      response = bl.InsertDetailBC(request); 

if (response.result) 
{ 
    if (IsImageChanged) 
    { 
     if(!Directory.Exists(Filepath)) 
      Directory.CreateDirectory(Filepath); 

     if(System.IO.File.Exists(Filepath+txt_val.Text.ToUpper().Trim() + ext)) 
      System.IO.File.Delete(Filepath + txt_val.Text.ToUpper().Trim() + ext); 

    } 

    pb_Image.Image.Save(Filepath + txt_val.Text.ToUpper().Trim() + ext); 
} 

在執行此我得到錯誤System.IO.File.Delete(); 的過程,因爲它正被另一個進程無法訪問該文件'E:\NewFolder\img.jpg'

+0

上面的代碼看起來很好。但是這看起來像一個winforms應用程序,所以必須有一些GUI參與。如果您的用戶界面目前正在顯示您要刪除的圖像,則必須先刪除該圖像的圖像和引用,然後再刪除它 – cleftheris

+0

是的,我做過。我仍然得到同樣的錯誤。 :( – Sowndarya

+0

您是否可以更新您的問題以包含顯示圖像的UI代碼片段? – cleftheris

回答

0

其他進程需要停止使用該進程。像Handles.exe和Process Explorer(都來自SysInternals)的工具可以識別哪些進程打開了文件。

如果您希望兩個進程不使用文件阻塞彼此,那麼進程需要一起工作(即,您不能強制共享:除非其他進程知道共享它,否則很可能會導致數據損壞並且崩潰)。

+0

我關閉了所有打開的進程,但仍然出現錯誤 – Sowndarya

+1

如果關閉了系統崩潰的所有進程,更多的進程比進程資源管理器顯示的應用程序選項卡更多。您需要找到哪些進程可以處理打開的文件。 – Richard