locked
MODI - Cannot delete file after document closed RRS feed

  • Question

  • User-930955590 posted

    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);
                    }
            }

    }

    [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.

    Wednesday, August 16, 2006 9:29 AM

All replies

  • User-930955590 posted

    I have found a work around for this issue. You can read about it by viewing this thread:

    http://shrinkster.com/hh3

    Thursday, August 17, 2006 10:26 AM
  • User1893801968 posted

    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.

    Monday, October 16, 2006 7:20 PM
  • User-930955590 posted

    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.

    Tuesday, October 17, 2006 1:44 PM
  • User1893801968 posted

    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);

    Tuesday, October 17, 2006 2:02 PM
  • User1893801968 posted

    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.

    Tuesday, October 17, 2006 6:54 PM
  • User-930955590 posted

    Hi Jason,

    Thanks for warning me. I have not had a chance to try your solution yet and now I will not try.

    Wednesday, October 18, 2006 12:36 PM
  • User-930955590 posted

    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. 

    Wednesday, September 19, 2007 1:18 PM
  • User230766423 posted

    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?

       

    Friday, August 14, 2009 6:47 AM