MODI - Cannot delete file after document closed

T

Tod Birdsall

I am using Microsoft Office Document Imaging (MODI) with great success.
However, when execute the following code, I get the error "The process
cannot access the file 'myFile.tif'" (File name has been shortened).

Here is the code I am using:
-- BEGIN CODE --
public class OCRUtil : IDisposable
{
const string FILENAME = "myFile.tif";
private MODI.Document doc = null;

/// <summary>
/// Initializes a new instance of the OCRUtil class.
/// </summary>
public OCRUtil(string filePath)
{
doc = new MODI.Document();
doc.Create(filePath);
doc.OCR(MODI.MiLANGUAGES.miLANG_SYSDEFAULT, true, true);
// Using SaveAs() rather than Save() so that if other classes
// wish to use the same file, we don't have to worry about a
// contention.
doc.SaveAs(FILENAME, MODI.MiFILE_FORMAT.miFILE_FORMAT_TIFF,
MODI.MiCOMP_LEVEL.miCOMP_LEVEL_HIGH);
}

public int GetImageCount()
{
return doc.Images.Count;
}

public void Dispose()
{
doc.Close(false);
doc = null;
GC.Collect();
if (File.Exists(FILENAME))
{
File.Delete(FILENAME);
}
}
}

[TestFixture]
public class OCRUtilTests
{
private string _filePath = @"..\..\2PageJournalFax.tif";
[Test]
public void TestOCRCount()
{
OCRUtil ocr = new OCRUtil(_filePath);

Assert.AreEqual(2, ocr.GetImageCount());

ocr.Dispose();
}
}

-- END CODE --

Here is the whole error message:

-- BEGIN ERROR MESSAGE --
TestCase 'Kahuna.OCR.Tests.OCRUtilTests.TestOCRRead'
failed: System.IO.IOException : The process cannot access the file
'D:\_Code\_Applications\KahunaOCR\Kahuna.OCR.Tests\bin\Debug\myFile.tif'
because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.Delete(String path)
D:\_Code\_Applications\KahunaOCR\Kahuna.OCR\OCRUtil.cs(47,0): at
Kahuna.OCR.OCRUtil.Dispose()
D:\_Code\_Applications\KahunaOCR\Kahuna.OCR.Tests\OCRUtilTests.cs(21,0):
at Kahuna.OCR.Tests.OCRUtilTests.TestOCRRead()
-- END ERROR MESSAGE --

Any ideas on how I can get the Document object to release its evil grip
on my defenseless file?

Thank you for your time.

Tod Birdsall, MCSD for .NET
blog: http://tod1d.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top