title: Directory.Move(): Access to Path is Denied
body: <p>I'm writing this Windows Form Application in Visual Studio 2010 using C#.</p>
  
  <p>There is a Execute button on the form, the user will hit the button, the program will generate some files and are stored in the <code>Output</code> folder (which is created by the program using <code>Directory.CreateDirectory()</code>)</p>
  
  <p>I want to create an Archive folder to save the output files from previous runs.</p>
  
  <p>In the beginning of each run, I try to move the existing <code>Output</code> folder to the <code>Archive</code> folder, then create a new <code>Output</code> folder. Below is the function I ran to move directory.</p>
  
  <pre><code>static void moveToArchive()
  {
      if (!Directory.Exists("Archive")) Directory.CreateDirectory("Archive");
      string timestamp = DateTime.Now.ToString("yyyyMMddHHmms");
      try
      {
          Directory.Move("Output", "Archive\\" + timestamp);
      }
      catch(Exception e)
      {
          Console.WriteLine("Can not move folder: " + e.Message);
      }
  }
  </code></pre>
  
  <p>The problem I ran into confuses me a lot...</p>
  
  <p>There are some times that I can successfully move the Output folder to archive, but sometimes it fails.</p>
  
  <p>The error message I got from catching the exception is <code>Access to path 'Output' is denied.</code></p>
  
  <p>I have checked that all the files in the <code>Output</code> folder are not in use. I don't understand how access is denied sometimes and not all the times.</p>
  
  <p>Can someone explain to me and show me how to resolve the problem?</p>
  
  <p><strong>--Edit--</strong></p>
  
  <p>After HansPassant comment, I modified the function a little to get the current directory and use the full path. However, I'm still having the same issue.</p>
  
  <p>The function now looks like this:</p>
  
  <pre><code>static void moveToArchive()
  {
      string currentDir = Environment.CurrentDirectory;
      Console.WriteLine("Current Directory = " + currentDir);
      if (!Directory.Exists(currentDir + "\\Archive")) Directory.CreateDirectory(currentDir + "\\Archive");
      string timestamp = DateTime.Now.ToString("yyyyMMddHHmms");
      try
      {
          Directory.Move(currentDir + "\\Output", currentDir + "\\Archive\\" + timestamp);
      }
      catch(Exception e)
      {
          Console.WriteLine("Can not move folder: " + e.Message);
      }
  }
  </code></pre>
  
  <p>I printed out the current directory and it is just as what I was expecting, and I'm still having trouble using full path. <code>Access to path 'C:\Users\Me\Desktop\FormApp\Output' is denied.</code></p>
  
  <p><strong>--Edit--</strong></p>
  
  <p>Thank you everyone for answering and commenting.</p>
  
  <p>I think some of you miss this part so I'm going stress it a bit more.</p>
  
  <p><strong>The Directory.Move() sometimes work and sometimes fails.</strong></p>
  
  <p>When the function succeed, there was no problem. <code>Output</code> folder is moved to <code>Archive</code></p>
  
  <p>When the function fails, the exception message I got was <em>Access to path denied.</em></p>