title: .Net Runtime Error Stopping Service; Unhandled IOException
body: <p>I have been put in charge of updating a service that reads a text file and creates pdf files from sections of the text file and emails out the pdfs. I recently made some changes to the service and used the .Net 4.0 Framework. When updating on the server, the 4.0 Framework was installed before I could move my files and start the service successfully - it was using 2.0 previously. The service runs until it reaches code that attempts to clean up the directory with the pdf files. The service Stops when the code attempts to delete a pdf file which is "in use by another process". The code looks for this exception and is supposed to wait for about 30 seconds and try the delete again. Locally, I run this service through a test Harness and it loops through until the file becomes available to delete (on average it takes about 5 minutes), but on the server the Service stops with the Unhandled IOException found in the Application Event log. I don't understand why it will continue processing locally but not on the server. Any ideas or other information would greatly be appreciated.</p>
  
  <p>Here is the error in the Event Log:</p>
  
  <p>The process was terminated due to an unhandled exception.</p>
  
  <blockquote>
    <p>Exception Info: System.IO.IOException
    Stack:
       at System.IO.__Error.WinIOError(Int32, System.String)
       at System.Console.GetBufferInfo(Boolean, Boolean ByRef)
       at processorName.FileProcessingThread.CleanUpDirectory(System.Object)
       at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
       at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
       at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
       at System.Threading.ThreadHelper.ThreadStart(System.Object)</p>
  </blockquote>
  
  <p>Also this error appears in the event log as well:</p>
  
  <blockquote>
    <p>EventType clr20r3, P1 myServiceName.exe, P2 1.0.1.0, P3 4db6e85d, P4 mscorlib, P5 4.0.0.0, P6 4d53693b, P7 3dab, P8 23b, P9 system.io.ioexception, P10 NIL.</p>
  </blockquote>
  
  <p>Here is the code that is supposed to handle the exception, wait and retry to delete the files.</p>
  
  <pre><code>while (!isDone)
  {
      bool myRetVal = true;
      string myErrorMsg = String.Empty;
      string myFile = String.Empty;
      //give it time to finish processing
      Thread.Sleep(30 * 1000);
      //
      //delete Files in folder
      foreach (string file in Directory.GetFiles(myDirectory, "*.PDF"))
      {  //delete all the *.PDF files
          try
          {
              File.Delete(file);
          }
          catch (Exception ex)
          {
              myErrorMsg = "...\r\n" + ex.Message;
              m_Log.Writeline("Unable to delete "+ file + ". MESSAGE:"+ myErrorMsg,3);
              myFile = file;
              myRetVal = false;
          }
      }
      //
      //now move the in-progress File to the processed directory
      if (myRetVal)
      {
          foreach (string file in Directory.GetFiles(myDirectory, "*.InProgress"))
          {
              try
              {
                  if (File.Exists(m_ConfigFile.ProcessedFolderName + Path.GetFileNameWithoutExtension(file) + ".done"))
                  {
                      File.Delete(file);
                  }
                  else
                  {
                      File.Move(file, m_ConfigFile.ProcessedFolderName + Path.GetFileNameWithoutExtension(file) + ".done");
                  }
              }
              catch (Exception ex)
              {
                  if (ex.Message.Contains("file already exists"))
                  {
  
                  }
                  else
                  {
                      myErrorMsg = "...\r\n" + ex.Message;
                      myFile = file;
                      myRetVal = false;
                  }
              }
          }
      }
      //
      //if empty, delete the SendMailFolder subfolder
      if (myRetVal)
      {
          try
          {
              if (Directory.GetFiles(myDirectory, "*.*").Length == 0) Directory.Delete(myDirectory);
          }
          catch (Exception ex)
          {
              myErrorMsg = "...\r\n" + ex.Message;
              myFile = myDirectory;
              myRetVal = false;
          }
      }
  
      if (Console.CursorLeft &gt; 0) Console.WriteLine("\r\n");
  
      if (myRetVal)
      {
          Console.WriteLine(DateTime.Now.ToLocalTime() + " CleanUp SendMailFolder...Done");
          isDone = true;
      }
      else
      {
          if (myErrorMsg.Contains("is being used by another process."))
          {
              myErrorMsg = " is still in use.";
              Console.WriteLine(DateTime.Now.ToLocalTime() + " CleanUp SendMailFolder..." + Path.GetFileName(myFile) + myErrorMsg);
          }
          else
          {
              Console.WriteLine(DateTime.Now.ToLocalTime() + " CleanUp SendMailFolder..." + Path.GetFileName(myFile) + myErrorMsg);
              m_Log.Writeline(DateTime.Now.ToLocalTime() + " CleanUp SendMailFolder..." + Path.GetFileName(myFile) + myErrorMsg, 3);
              isDone = true;
          }
      }
  }
  </code></pre>