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(file Path);
doc.OCR(MODI.Mi LANGUAGES.miLAN G_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(FILE NAME, MODI.MiFILE_FOR MAT.miFILE_FORM AT_TIFF,
MODI.MiCOMP_LEV EL.miCOMP_LEVEL _HIGH);
}
public int GetImageCount()
{
return doc.Images.Coun t;
}
public void Dispose()
{
doc.Close(false );
doc = null;
GC.Collect();
if (File.Exists(FI LENAME))
{
File.Delete(FIL ENAME);
}
}
}
[TestFixture]
public class OCRUtilTests
{
private string _filePath = @"..\..\2PageJo urnalFax.tif";
[Test]
public void TestOCRCount()
{
OCRUtil ocr = new OCRUtil(_filePa th);
Assert.AreEqual (2, ocr.GetImageCou nt());
ocr.Dispose();
}
}
-- END CODE --
Here is the whole error message:
-- BEGIN ERROR MESSAGE --
TestCase 'Kahuna.OCR.Tes ts.OCRUtilTests .TestOCRRead'
failed: System.IO.IOExc eption : The process cannot access the file
'D:\_Code\_Appl ications\Kahuna OCR\Kahuna.OCR. Tests\bin\Debug \myFile.tif'
because it is being used by another process.
at System.IO.__Err or.WinIOError(I nt32 errorCode, String maybeFullPath)
at System.IO.File. Delete(String path)
D:\_Code\_Appli cations\KahunaO CR\Kahuna.OCR\O CRUtil.cs(47,0) : at
Kahuna.OCR.OCRU til.Dispose()
D:\_Code\_Appli cations\KahunaO CR\Kahuna.OCR.T ests\OCRUtilTes ts.cs(21,0):
at Kahuna.OCR.Test s.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