簡體   English   中英

C# File.Delete,另一個進程正在使用的文件

[英]C# File.Delete, file being used by another process

我在嘗試刪除圖像文件時遇到問題。 我總是收到一條錯誤消息:IOExeption 未處理。 訪問被拒絕,因為該文件正被另一個進程使用。

我不知道這可能是什么過程以及如何解決它。

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {            
            Album album = GetAlbum(comboBox1.SelectedIndex);
            Photo photo = GetPhoto(comboBox1.SelectedIndex, comboBox3.SelectedIndex);           

            txtPhotoPath.Text = Directory.GetCurrentDirectory() + "\\"  + photo.SPath;

            lblExtention.Text = photo.SExtention;
            txtPhotoTitle.Text = photo.STitle;
            pctrbFoto.Image = Image.FromFile(foto.SPath).GetThumbnailImage(GetWitdth(photo.SPath, GetHeight(photo.SPath, 150)), GetfHeight(photo.SPath, 150), null, new IntPtr());
        }

private void btnChangePhoto_Click(object sender, EventArgs e)
{
            Album album = GetAlbum(comboBox1.SelectedIndex);
            Photo photo = GetPhoto(comboBox1.SelectedIndex, comboBox3.SelectedIndex);

            File.Delete("Albums\\Images\\" + photo.STitle + foto.SExtention);

            photo.SExtention = lblExtention.Text;
            photo.STitle = txtPhotoTitel.Text;
            Photo.SPath = txtPath.Text;

            File.Copy(photo.SPath, "Albums\\Images\\" + photo.STitle + photo.SExtention);

}

謝謝,文茲森特


感謝大家的幫助。

我用過這個,現在效果很好


您的進程是使用 file 的進程,您需要將圖像設置為 null 使用以下內容:

var img = Image.FromFile(foto.SPath).GetThumbnailImage(GetWitdth(photo.SPath, GetHeight(photo.SPath, 150)), GetfHeight(photo.SPath, 150), null, new IntPtr());

pcrtrbFoto.Image = img;

圖像 = 空;

GC.Collect();

我要查看的第一個區域是您的 GetPhoto 方法。 您是否有尚未關閉的 StreamReader? 請確保如果您在刪除之前對文件進行任何類型的 I/O,請先關閉這些連接。 GetPhoto() 方法有什么作用?

首先,您需要確定是您的應用程序還是另一個打開了該文件的應用程序。

您可以使用 Mark Russinovich 的 Process Explorer 查看哪個程序打開了特定的文件或目錄。 它是 Windows Sysinternals 優秀實用程序系列的一部分,每個程序員/IT 專業人員都應該使用(或至少知道)。

你可以在這里得到它: http : //technet.microsoft.com/en-us/sysinternals/bb896653.aspx

您在哪里使用縮略圖:

using(Image img = Image.FromFile(foto.SPath))
{
  pctrbPhoto. Image = img.GetThumbnailImage(
  GetWitdth(photo.SPath, GetHeight(photo.SPath, 150)), 
  GetfHeight(photo.SPath, 150), null, new IntPtr()); 
}

而是確保在您完成處理后處理(關閉)源圖像。

他們以您的方式擁有它,從文件加載的圖像會一直存在,直到垃圾收集器決定釋放它,這可能需要一些時間。

用 FromFile 加載的圖像保存它們從打開加載的文件。

當您在comboBox3_SelectedIndexChanged調用Image.FromFile時,也可能在其他地方調用Image.FromFile時,您不會處理Image對象。 因此,您的程序正在使用該文件。

每次打開圖像時都需要處理圖像。

您的進程是使用 file 的進程,您需要將圖像設置為 null 使用以下內容:

using(var img = Image.FromFile(foto.SPath).GetThumbnailImage(GetWitdth(photo.SPath, GetHeight(photo.SPath, 150)), GetfHeight(photo.SPath, 150), null, new IntPtr()))
  pctrbFoto.Image = img;

當所有其他方法都失敗時,您可以使用MoveFileEx在下次重新啟動時刪除該文件。

...但是,如果您的應用程序在網絡托管計划上運行? 您不能在共享服務器中運行任何軟件。

我已經嘗試過使用 dispose() 和其他選項,但我無法刪除像 Vinzcent 這樣的文件。

馬爾迪托 IIS :@

嘗試從同一軟件中刪除/修改以前保存的文件時會出現此問題。 我通過添加以下代碼解決了它:

System.GC.Collect(); 
System.GC.WaitForPendingFinalizers();


File.Delete(MyFile);

您可以使用 Unlocker 程序來告訴您哪些程序鎖定了文件

注意:刪除了解鎖程序的鏈接 - 包含惡意軟件。

我曾經使用過類似thestarSadegh發布的內容,但在某些情況下它不起作用/有幫助,所以我找到了一個不同的解決方案,我已經在這里發布了

仍然在這里代碼(您可能會在觀看鏈接和問題后更好地理解它):

   var imageAsByteArray = File.ReadAllBytes(imagePath);

   // I use as example a pictureBox:
   pictureBox1.Image = byteArrayToImage(imageAsByteArray);

   // Or/and safe/copy/replace it:
   File.WriteAllBytes(picture_Path, imageAsByteArray);

您也可以立即刪除(新)圖片! (如果你想)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM