If there is a beter forum for this question, please let me know and I will move it there.
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);
}
}
</div>[TestFixture]
public class OCRUtilTests
{
private string _filePath = @"..\..\2PageJournalFax.tif";
[Test]
public void TestOCRCount()
{
OCRUtil ocr = new OCRUtil(_filePath);
-- 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?
I tried all different suggetions on internet, but still cannot delete the document (see code below). Can somebody please help? It blocks a major project I am working on.
I finally figured a way to delete the file (see below). Please let me know if it works for you.
The same code didn't work when I had a foreach loop on 'images'. After I changed to loop through an array, it started to work. Very strange. However, I am just happy I have found a way that can release the file. Otherwise, I would need to change a lot of
code in my project.
Hope it works for youu too.
Thanks
-Jason
----------------------------------------------
MODI.Document faxMODIDocument =
new MODI.Document();
I have had similar problems in the past and they were solved by not
only carefully releasing the objects and setting them to null as you
do here, but also creating a separate worker thread that is the owner
of the MODI objects. This thread declares, instantiates, and destroys
the MODI objects.
When it exits, all should be released.
Hope that helps someone!!
---------------------------------
I have not tested it yet, but this may solve the issue.
tod1d
Member
79 Points
19 Posts
MODI - Cannot delete file after document closed
Aug 16, 2006 01:29 PM|LINK
Hi All,
If there is a beter forum for this question, please let me know and I will move it there.
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);
}
}
<div class=qt id=qhide_79278 style="DISPLAY: block">}
</div>[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();
}
<div class=qt id=qhide_79279 style="DISPLAY: block">}
</div>-- 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.
blog: http://tod1d.net
tod1d
Member
79 Points
19 Posts
Re: MODI - Cannot delete file after document closed
Aug 17, 2006 02:26 PM|LINK
I have found a work around for this issue. You can read about it by viewing this thread:
http://shrinkster.com/hh3
blog: http://tod1d.net
jasonxue
Member
40 Points
9 Posts
Re: MODI - Cannot delete file after document closed
Oct 16, 2006 11:20 PM|LINK
I tried all different suggetions on internet, but still cannot delete the document (see code below). Can somebody please help? It blocks a major project I am working on.
Thanks
-Jason
faxMODIDocument.Close(false);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(faxMODIDocument);
System.Runtime.InteropServices.Marshal.ReleaseComObject(faxMODIDocument);
System.Runtime.InteropServices.Marshal.ReleaseComObject(images);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(images);
faxMODIDocument = null;
images = null;
GC.Collect();
System.IO.File.Delete(fileName); //Exception still thrown - file being used.
tod1d
Member
79 Points
19 Posts
Re: MODI - Cannot delete file after document closed
Oct 17, 2006 05:44 PM|LINK
Hi Jason,
As my "groups" post suggests, I was unable to determine how to delete the file. However, I was able to reuse the file, which served my purposes.
When you determine a solution to your problem, please share it.
Thanks.
blog: http://tod1d.net
jasonxue
Member
40 Points
9 Posts
Re: MODI - Cannot delete file after document closed
Oct 17, 2006 06:02 PM|LINK
I finally figured a way to delete the file (see below). Please let me know if it works for you.
The same code didn't work when I had a foreach loop on 'images'. After I changed to loop through an array, it started to work. Very strange. However, I am just happy I have found a way that can release the file. Otherwise, I would need to change a lot of code in my project.
Hope it works for youu too.
Thanks
-Jason
----------------------------------------------
MODI.Document faxMODIDocument = new MODI.Document();
faxMODIDocument.Create(fileName);
MODI.Images images = faxMODIDocument.Images;
faxMODIDocument.Close(false)
faxMODIDocument = null;
images = null;
GC.Collect();
System.IO.File.Delete(fileName);
jasonxue
Member
40 Points
9 Posts
Re: MODI - Cannot delete file after document closed
Oct 17, 2006 10:54 PM|LINK
Bad news! The code I posted does not always work. It works randomly.
I cannot believe that there is no single solution for releasing a MODI document.
tod1d
Member
79 Points
19 Posts
Re: MODI - Cannot delete file after document closed
Oct 18, 2006 04:36 PM|LINK
Hi Jason,
Thanks for warning me. I have not had a chance to try your solution yet and now I will not try.
blog: http://tod1d.net
tod1d
Member
79 Points
19 Posts
Re: MODI - Cannot delete file after document closed
Sep 19, 2007 05:18 PM|LINK
I just received the following email:
-----------------------------------
Hello all,
I have had similar problems in the past and they were solved by not
only carefully releasing the objects and setting them to null as you
do here, but also creating a separate worker thread that is the owner
of the MODI objects. This thread declares, instantiates, and destroys
the MODI objects.
When it exits, all should be released.
Hope that helps someone!!
---------------------------------
I have not tested it yet, but this may solve the issue.
blog: http://tod1d.net
Swati Jain
Participant
1353 Points
749 Posts
Re: MODI - Cannot delete file after document closed
Aug 14, 2009 10:47 AM|LINK
Hello all,
string strText = "";
// Instantiate the MODI.Document object
MODI.Document md = new MODI.Document();
// The Create method grabs the picture from
// disk snd prepares for OCR.
string strFileName = Session["ImagePath"].ToString();
md.Create(strFileName);---I get the error The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))
// Do the OCR.
md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
// Get the first (and only image)
MODI.Image image = (MODI.Image)md.Images[0];
// Get the layout.
MODI.Layout layout = image.Layout;
// Loop through the words.
for (int j = 0; j < layout.Words.Count; j++)
{
// Get this word.
MODI.Word word = (MODI.Word)layout.Words[j];
// Add a blank space to separate words.
if (strText.Length > 0)
{
strText += "";
}
// Add the word.
strText += " "+word.Text;
}
// Close the MODI.Document object.
// md.Images.Remove(image);
//image = null;
// image = new MODI.Image();
// md = new MODI.Document();
md.Close(false);
md = null;
image = null;
GC.Collect();
TextBox1.Text = strText;
Pls suggest me ,whats goin wrong with above code?