簡體   English   中英

如何在winform中刪除IronOCR進程正在使用的文件?

[英]How can i deleted a file that is using by IronOCR process in winform?

對不起,我的英語不好。 我想從圖像中提取文本然后將其刪除。 在我從文件中提取文本以將其刪除后,您能告訴我如何停止 Ocr 進程嗎? 這是我的代碼

Image image = Image.FromFile(@"C:\Users\Admin\Desktop\testtool.png");
        var ocr = new AutoOcr();
        result_text.Text = ocr.Read(image).ToString();
        string textforClipboard = result_text.Text.Replace("\n", Environment.NewLine);
        Clipboard.Clear();
        Clipboard.SetText(textforClipboard);
        //File.Delete(@"C:\Users\Admin\Desktop\testtool.png");

這是錯誤: System.IO.IOException: 'The process cannot access the file 'C:\\Users\\Admin\\Desktop\\testtool.png' 因為它正被另一個進程使用。

圖像是一次性的。 在刪除包含圖像的文件之前,您應該處理從文件中提取的所有圖像。

讓它成為一個好習慣:在處理 IDisposable 時總是考慮using 這樣,您就可以確定無論發生什么:當您不再需要該對象時,即使發生異常,該對象也會被釋放。

using (Image image = Image.FromFile(@"C:\Users\Admin\Desktop\testtool.png"))
{
     // do with the image what you need to do
     // Is AutoOcr also IDisposable?
     using (var autoOcr = new AutoOcr())
     {
         ...
     }
     // object autoOcr is disposed
}

// object image is disposed. You can delete the file that contains this image
System.IO.File.Delete(...);

暫無
暫無

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

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