title: Process cannot access the file because it is being used by another process on Directory.Move()
body: <p>We have a client application with the following architecture: a manager process manages a couple of worker processes (reader and writer) and periodically queries the server for version updates. If a version update is available, the manager downloads it to the client computer, shuts down the worker threads, starts an updater process to handle the update and exits. The updater, on startup, receives the manager PID and the update file location; it then waits for the manager to exit, backs up all files of the manager and workers, recreates their directories and spreads the new version files to the new directories.</p>
  
  <p>When going through this process as described, the first call to <code>Directory.Move(string, string)</code> – which serves to back up the manager directory – throws the <code>IOException</code>. The strange thing is, if I let the manager shut down without starting the updater and then start the updater executable myself, the exception is not thrown.</p>
  
  <p>Manager code for managing worker threads:</p>
  
  <pre><code>public void Run()
  {
     _config = GetConfiguration();
     Process reader, writer;
  
     //Start reader and writer with appropriate arguments
  
     //Keep reader and writer alive
  
     reader.Kill();
     writer.Kill();
  
     reader.WaitForExit();
     writer.WaitForExit();
  
     reader.Dispose();
     writer.Dispose();
  }
  </code></pre>
  
  <p>Manager code for querying the database:</p>
  
  <pre><code>EndpointAddress endpoint;
  BasicHttpBinding httpBinding = new BasicHttpBinding();
  httpBinding.MaxReceivedMessageSize = 2000000000;
  ChannelFactory&lt;IService&gt; chanFactory = new ChannelFactory&lt;IService&gt;(httpBinding);
  IService service;
  
  try
  {
     endpoint = new EndpointAddress(ConfigurationManager.AppSettings["Service URL"]);
  
     service = chanFactory.CreateChannel(endpoint);
  
     UpdateInstructions instructions = service.GetUpdateInstructions(_config.SiteID,     Assembly.GetExecutingAssembly().GetName().Version.ToString(), _config.Version);
  
     HandleUpdateInstructions(instructions); //Downloads files and starts the updater process
  }
  catch (Exception ex)
  {
     //Report exception
  }
  finally
  {
      if (chanFactory.State != CommunicationState.Faulted)
      chanFactory.Close();
  }
  </code></pre>
  
  <p>Manager code for starting the updater process:</p>
  
  <pre><code>private void StartUpdater(string updateFilePath, string configFilePath)
  {
     ProcessStartInfo updaterStartInfo = new ProcessStartInfo(_config.UpdaterExePath, string.Format("{0} \"{1}\" \"{2}\"", Process.GetCurrentProcess().Id, updateFilePath, configFilePath));
     Process updater = Process.Start(updaterStartInfo);
     updater.Dispose();
  }
  </code></pre>
  
  <p>Updater code for waiting for the manager to close:</p>
  
  <pre><code>bool isManagerUp = true;
  
  while (isManagerUp)
  {
    try
    {
       Process managerProcess = Process.GetProcessById(bDoxForceManagerPID);
  
       managerProcess.WaitForExit();
  
       managerProcess.Dispose();
  
       isManagerUp = false;
    }
    catch
    {
       isManagerUp = false;
    }
  }
  </code></pre>
  
  <p>Updater code for updating a module:</p>
  
  <pre><code>//updateDirectory is the directory of the new files to be inserted, moduleDirectory is the working directory of the module that will be updated, in this case the manager
  private void UpdateModule(DirectoryInfo updateDirectory, DirectoryInfo moduleDirectory)           
  {
     string backupDirectory = MakeBackupDirectoryFullPath(moduleDirectory.Parent.FullName);
  
     Directory.Move(moduleDirectory.FullName, backupDirectory); // IOException as described above.
     Directory.CreateDirectory(moduleDirectory.FullName);
  
     foreach (FileInfo updateFile in updateDirectory.EnumerateFiles())
     {
         string newFilePath = moduleDirectory.FullName + "\\" + updateFile.Name;
  
         File.Copy(updateFile.FullName, newFilePath);
     }
  
     Directory.Delete(updateDirectory.FullName, true);
  }
  </code></pre>