Last post Aug 14, 2009 06:47 AM by Swati Jain
Aug 16, 2006 09:29 AM|tod1d|LINK
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;
/// Initializes a new instance of the OCRUtil class.
public OCRUtil(string filePath)
doc = new MODI.Document();
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
public int GetImageCount()
public void Dispose()
doc = null;
<div class=qt id=qhide_79278 style="DISPLAY: block">}
public class OCRUtilTests
private string _filePath = @"..\..\2PageJournalFax.tif";
public void TestOCRCount()
OCRUtil ocr = new OCRUtil(_filePath);
<div class=qt id=qhide_79279 style="DISPLAY: block">}
</div>-- END CODE --
Here is the whole error message:
-- BEGIN ERROR MESSAGE --
failed: System.IO.IOException : The process cannot access the file
because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.Delete(String path)
-- 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.
Aug 17, 2006 10:26 AM|tod1d|LINK
I have found a work around for this issue. You can read about it by viewing this thread:
Oct 16, 2006 07:20 PM|jasonxue|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.
faxMODIDocument = null;
images = null;
System.IO.File.Delete(fileName); //Exception still thrown - file being used.
Oct 17, 2006 01:44 PM|tod1d|LINK
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.
Oct 17, 2006 02:02 PM|jasonxue|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.
MODI.Document faxMODIDocument =
MODI.Images images = faxMODIDocument.Images;
Oct 17, 2006 06:54 PM|jasonxue|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.
Oct 18, 2006 12:36 PM|tod1d|LINK
Thanks for warning me. I have not had a chance to try your solution yet and now I will not try.
Sep 19, 2007 01:18 PM|tod1d|LINK
I just received the following email:
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.
Aug 14, 2009 06:47 AM|Swati Jain|LINK
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;
// 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.
//image = null;
// image = new MODI.Image();
// md = new MODI.Document();
md = null;
image = null;
TextBox1.Text = strText;
Pls suggest me ,whats goin wrong with above code?