title: Delete a directory where someone has opened a file
body: <p>I am trying to programmatically delete and replace the contents of an application, "App A", using an "installer" program, which is just a custom WPF .exe app, we'll call "App B".  (My question concerns code in "App B".)</p>
  
  <p><strong>GUI Setup (not particularly important)</strong><br>
  App B has a GUI where a user can pick computer names to copy App A onto.  A file picker is there the admin uses to fill in the source directory path on the local machine by clicking "App A.exe".  There are also textboxes for a user name and password, so the admin can enter their credentials for the target file server where App A will be served - the code impersonates the user with these to prevent permission issues.  A "Copy" button starts the routine.  </p>
  
  <p><strong>Killing App A, File Processes, and Doing File Deletion</strong><br>
  The Copy routine starts by killing the "App A.exe" process on all computers in the domain, as well as explorer.exe, in case they had App A's explorer folder open.  Obviously this would be done afterhours, but someone may still have left things open and locked their machine before going home.  And that's really the base of the problem I'm looking to solve.</p>
  
  <p>Prior to copying over the updated files, we want to delete the entire old directory. In order to delete the directory (and its subdirectories), each file within them has to be deleted.  <strong>But say they had a file open from App A's folder.</strong>  The code finds any locking process on any file prior to deleting it (using code from Eric J.'s answer at <a href="https://stackoverflow.com/questions/317071/how-do-i-find-out-which-process-is-locking-a-file-using-net">How do I find out which process is locking a file using .NET?</a> ), it kills that process on whatever computer it is running on.  If local, it just uses:</p>
  
  <pre><code>public static void localProcessKill(string processName)
  {
      foreach (Process p in Process.GetProcessesByName(processName))
      {
          p.Kill();
      }
  }
  </code></pre>
  
  <p>If remote, it uses WMI:</p>
  
  <pre><code>public static void remoteProcessKill(string computerName, string fullUserName, string pword, string processName)
  {
      var connectoptions = new ConnectionOptions();
      connectoptions.Username = fullUserName;  // @"YourDomainName\UserName";
      connectoptions.Password = pword;
  
      ManagementScope scope = new ManagementScope(@"\\" + computerName + @"\root\cimv2", connectoptions);
  
      // WMI query
      var query = new SelectQuery("select * from Win32_process where name = '" + processName + "'");
  
      using (var searcher = new ManagementObjectSearcher(scope, query))
      {
          foreach (ManagementObject process in searcher.Get()) 
          {
              process.InvokeMethod("Terminate", null);
              process.Dispose();
          }
      }
  }
  </code></pre>
  
  <p>Then it can delete the file.  All is well.</p>
  
  <p><strong>Directory Deletion Failure</strong><br>
  In my code below, it is doing the recursive deletion of the files, and does it fine, up until the <code>Directory.Delete()</code>, where it will say <code>The process cannot access the file '\\\\SERVER\\C$\\APP_A_DIR' because it is being used by another process</code>, because I am attempting to delete the directory while I had a file still open from it (even though the code was actually able to delete the physical file-the instance is still open).</p>
  
  <pre><code>    public void DeleteDirectory(string target_dir)
      {
          string[] files = Directory.GetFiles(target_dir);
          string[] dirs = Directory.GetDirectories(target_dir);
          List&lt;Process&gt; lstProcs = new List&lt;Process&gt;();
  
          foreach (string file in files)
          {
              File.SetAttributes(file, FileAttributes.Normal);
              lstProcs = ProcessHandler.WhoIsLocking(file);
              if (lstProcs.Count == 0)
                  File.Delete(file);
              else  // deal with the file lock
              {
                  foreach (Process p in lstProcs)
                  {
                      if (p.MachineName == ".")
                          ProcessHandler.localProcessKill(p.ProcessName);
                      else
                          ProcessHandler.remoteProcessKill(p.MachineName, txtUserName.Text, txtPassword.Password, p.ProcessName);
                  }
                  File.Delete(file);
              }
          }
  
          foreach (string dir in dirs)
          {
              DeleteDirectory(dir);
          }
  
          //ProcessStartInfo psi = new ProcessStartInfo();
          //psi.Arguments = "/C choice /C Y /N /D Y /T 1 &amp; Del " + target_dir;
          //psi.WindowStyle = ProcessWindowStyle.Hidden;
          //psi.CreateNoWindow = true;
          //psi.FileName = "cmd.exe";
          //Process.Start(psi);
  
          //ProcessStartInfo psi = new ProcessStartInfo();
          //psi.Arguments = "/C RMDIR /S /Q " + target_dir; 
          //psi.WindowStyle = ProcessWindowStyle.Hidden;
          //psi.CreateNoWindow = true;
          //psi.FileName = "cmd.exe";
          //Process.Start(psi);
  
          // This is where the failure occurs
          //FileSystem.DeleteDirectory(target_dir, DeleteDirectoryOption.DeleteAllContents);
          Directory.Delete(target_dir, false);
      }
  </code></pre>
  
  <p>I've left things I've tried commented out in the code above.  While I can kill processes attached to the files and delete them, <strong>is there a way to kill processes attached to folders, in order to delete them?</strong></p>
  
  <p>Everything online I saw tries to solve this using a loop-check with a delay.  This will not work here.  I need to kill the file that was opened-which I do-but also ensure the handle is released from the folder so it can also be deleted, at the end.  Is there a way to do this?</p>
  
  <p><strong>Another option I considered that will not work:</strong>
  I thought I might just freeze the "installation" (copying) process by marking that network folder for deletion in the registry and schedule a programmatic reboot of the file server, then re-run afterwards.  <a href="https://stackoverflow.com/questions/25098180/how-to-delete-thumbs-db-it-is-being-used-by-another-process">How to delete Thumbs.db (it is being used by another process)</a> gives this code by which to do this:</p>
  
  <pre><code>[DllImport("kernel32.dll")]
  public static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, int dwFlags);
  
  public const int MOVEFILE_DELAY_UNTIL_REBOOT = 0x4;
  
  //Usage:
  MoveFileEx(fileName, null, MOVEFILE_DELAY_UNTIL_REBOOT);
  </code></pre>
  
  <p>But it has in the documentation that If <code>MOVEFILE_DELAY_UNTIL_REBOOT</code> is used, "the file cannot exist on a remote share, because delayed operations are performed before the network is available."  And that was assuming it might have allowed a folder path, instead of a file name.  (Reference:  <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa365240(v=vs.85).aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/windows/desktop/aa365240(v=vs.85).aspx</a> ).</p>