title: what process is locking the directory when try to change the LastWrite datetime?
body: <p>I am writing within a heavily managed async (single) thread. it is a FIFO queue in a thread and <code>something</code> is locking my attempt to do a <code>Directory.SetLastWriteTime</code>. It is almost certainly me. but I can't see it. I used <a href="https://stackoverflow.com/questions/317071/how-do-i-find-out-which-process-is-locking-a-file-using-net"><strong>this link</strong></a>: and as part of it I tried to use <a href="http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx" rel="nofollow noreferrer"><code>Handle</code></a> but c# wouldn't understand the reference to <code>handle.exe</code> (failed at line <code>tool.Start();</code>) despite the path to handle.exe being in the system path and it running fine in command.</p>
  
  <p>The problem is I then tried to use the <a href="https://stackoverflow.com/questions/317071/how-do-i-find-out-which-process-is-locking-a-file-using-net"><code>FileUtil</code></a> example code lower down the answers list but this only works on <code>files</code> I need to see what is locking a <code>folder</code>. bear in mind this folder is not really locked, I can change the <code>LastModifiedDate</code> in <code>Attribute Changer 7</code> so it is certainly my queue that is dong it wrong. </p>
  
  <p>this is the code I am actually trying to run, that continually says it is locked. </p>
  
  <pre><code>System.DateTime newDateTime = new DateTime(2011,1,1,0,0,0,0,System.DateTimeKind.Utc);
  if (Directory.Exists(parent))
  {
      Unlocker.unlock(parent); //&lt;--new line
      Directory.SetLastWriteTime(parent, newDateTime);
  }
  </code></pre>
  
  <p>note that I suspect <code>FileUtil</code> works fine for files but I need to modify a <code>folder</code> and it is bombing badly. Can anyone help me get the Process locking a given directory not just a file?</p>
  
  <p><strong>EDIT</strong> (see also, <code>&lt;&lt;- new line</code> above) I have abandoned <code>FileUtil</code> but I am now using  <code>handle.exe</code> successfully (i.e. if I manually lock it by trying to to give it mouse focus in windows explorer for example it detects it) but handle returns no "locks" when the c# bombs:</p>
  
  <pre><code>public static class Unlocker
  {
    public static void unlock(string fileName)
    {
      Process tool = new Process();
      tool.StartInfo.FileName = @"C:\Program Files (x86)\Handle\handle.exe";
      tool.StartInfo.Arguments = fileName + " /accepteula";
      tool.StartInfo.UseShellExecute = false;
      tool.StartInfo.RedirectStandardOutput = true;
      tool.Start();
      tool.WaitForExit();
      string outputTool = tool.StandardOutput.ReadToEnd();
  
      string matchPattern = @"(?&lt;=\s+pid:\s+)\b(\d+)\b(?=\s+)";
      foreach (Match match in Regex.Matches(outputTool, matchPattern))
      {
              Debug.WriteLine("Unlocker.unlock(`" + fileName + "`) is locked by `" + Process.GetProcessById(int.Parse(match.Value)).ProcessName + "`");
      }
    }
  }
  </code></pre>
  
  <p><strong>SECOND EDIT</strong>
  the SetWriteTime was working all along, it was a looping issue further up the code line, it didn't work when the user has mouse clicked on the folder (which I had been doing accidentally randomly) but the bad outer loop was perpetuating it - a bad finally forever causing a loop counter exception with a faulty "file is locked" message.</p>