为什么MOD​​I不允许我删除已处理的文件?

时间:2010-12-31 11:13:16

标签: c# file ocr delete-file modi

private void button4_Click(object sender, EventArgs e)
{
        OCR.recognize("test1.tif");
        System.IO.File.Delete("test1.tif"); // <--- Problem on this line
}

....

public static string recognize(string filepath, MODI.MiLANGUAGES language =
MODI.MiLANGUAGES.miLANG_RUSSIAN)
{
        if (!File.Exists(filepath)) return "error 1: File does not exist";
        MODI.Document doc = new MODI.Document();
        doc.Create(filepath);

        doc.OCR(language, true, true);
        MODI.Image image = (MODI.Image)doc.Images[0];

        string result="";
        foreach (MODI.Word worditems in image.Layout.Words)
        {
            result += worditems.Text + ' ';

        // Processed image is ALWAYS a question
            if (worditems.Text[worditems.Text.Length - 1] == '?') break;
        }


        doc.Close();

        return result;

}

问题是:文件由另一个进程使用。

如何在OCR后删除它?

1 个答案:

答案 0 :(得分:0)

有人为此posted a solution

public void Dispose()  
{   
        doc.Close(false); 
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(doc);
        doc = null; 
        GC.Collect();
}

也许它适合你的情况。