簡體   English   中英

c#ASP.NET如何刪除另一個進程“正在使用”的文件?

[英]c# ASP.NET How do i delete a file that is “in use” by another process?

我正在加載文件:

System.Drawing.Image img = System.Drawing.Image.FromFile(FilePath);

現在,我想保存圖像:

img.Save(SavePath);

除非FilePath == SavePath ,否則它將決定給我錯誤:

System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.

因此,我嘗試在打開文件后立即刪除該文件:

System.Drawing.Image img = System.Drawing.Image.FromFile(FilePath);
File.Delete(FilePath);

它給了我錯誤:

System.IO.IOException: The process cannot access the file 'filename.jpg' because it is being used by another process.

所以...如果某人不使用該文件,我該如何修改該文件?

圖像將一直保持鎖定,直到將其丟棄( 請參閱此處 )。

該文件將保持鎖定,直到處理完映像為止。

您必須將圖像保存在其他位置(或復制其內容),然后使用using子句處理打開的圖像。

例:

using(Image image1 = Image.FromFile("c:\\test.jpg"))
{
    image1.Save("c:\\test2.jpg");
}

System.IO.File.Delete("c:\\test.jpg");
System.IO.File.Move("c:\\test2.jpg", "c:\\test.jpg");

您可以使用內存流,也可以將其放入byte []數組中

http://www.vcskicks.com/image-to-byte.php

暫無
暫無

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

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