title: How to OCR serially with MODI (Microsoft Office Document Imaging) in C#
body: <p>I have documents scanned as .jpg pictures in a folder and I would like to do OCR in C# serially for each of my documents in that folder.
  so far ive done this : </p>
  
  <pre><code>public string CheckFilesAndDoOCR(string directoryPath)
  {
      directoryPath = Environment.SpecialFolder.MyPictures + "\\OCRTempPictures\\";
      IEnumerator files = Directory.GetFiles(directoryPath).GetEnumerator();
      string TheTxt = "";
  
      while (files.MoveNext())
      {
          // FileInfo
          FileInfo nfo = new FileInfo(Convert.ToString(files.Current));
  
          // Get new file name
          string fileName = AlltoJPG(nfo);
  
          // FileInfo (New File)
          FileInfo foo = new FileInfo(fileName);
  
          // Check for JPG File Format
          if (foo.Extension == ".jpg" || foo.Extension == ".JPG")
          // or // ImageFormat.Jpeg.ToString()
          {
              try
              {
                  // OCR Operations...
                  MODI.Document md = new MODI.Document();
                  md.Create(foo.FullName);
                  md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false); // OCR();
                  MODI.Image image = (MODI.Image)md.Images[0];
                  TheTxt = image.Layout.Text;
                  md.Close(false);
  
                  // Create text file with the same Image file name
                  FileStream createFile = new FileStream(foo.DirectoryName + "\\" + foo.Name.Replace(foo.Extension,string.Empty) + ".txt", FileMode.CreateNew);
  
                  // Save the image text in the text file
                  StreamWriter writeFile = new StreamWriter(createFile);
                  writeFile.Write(TheTxt);
                  writeFile.Close();
              }
              catch (Exception ex)
              {
                  // Expected errors
                  string LogPath = System.Environment.SpecialFolder.MyPictures + "\\OCRTempPictures\\OCRInfo.txt";
                  Logger(LogPath, "| Exception: Source[" + ex.Source + "] Message[" + ex.Message + "] InnerException[" + ex.InnerException + "] StackTrace[" + ex.StackTrace + "] | ");
                  // MessageBox.Show(ex.Message, "OCR Exception", MessageBoxButtons.OK, MessageBoxIcon.Information);
              }
          }
      }
      return TheTxt;
  }
  </code></pre>
  
  <p>but MODI gives the <code>OCR running!</code> or <code>Cant reach file.File is in use.</code> errors.. </p>
  
  <p><strong>Depending on the situation:</strong></p>
  
  <ul>
  <li><p>How can I avoid these error?</p></li>
  <li><p>Is there anyways to stop OCR action and deplete all objects in use?</p></li>
  </ul>
  
  <p>If anyone can answer any of the questions above it would be appreciated.</p>