title: About File permissions in C#
body: <p>While creating a file synchronization program in C# I tried to make a method <code>copy</code> in <code>LocalFileItem</code> class that uses <code>System.IO.File.Copy(destination.Path, Path, true)</code> method where <code>Path</code> is a <code>string</code>.<br>
  After executing this code with destination. <code>Path = "C:\\Test2"</code> and <code>this.Path = "C:\\Test\\F1.txt"</code> I get an exception saying that I do not have the required file permissions to do this operation on <strong>C:\Test</strong>, but <strong>C:\Test</strong> is owned by myself <em>(the current user)</em>.<br>
  Does anybody knows what is going on, or how to get around this?</p>
  
  <p>Here is the original code complete.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  using System.IO;
  
  namespace Diones.Util.IO
  {
      /// &lt;summary&gt;
      /// An object representation of a file or directory.
      /// &lt;/summary&gt;
      public abstract class FileItem : IComparable
  
      {
          protected String path;
          public String Path
          {
              set { this.path = value; }
              get { return this.path; }
          }
          protected bool isDirectory;
          public bool IsDirectory
          {
              set { this.isDirectory = value; }
              get { return this.isDirectory; }
          }
          /// &lt;summary&gt;
          ///  Delete this fileItem.
          /// &lt;/summary&gt;
          public abstract void delete();
          /// &lt;summary&gt;
          ///  Delete this directory and all of its elements.
          /// &lt;/summary&gt;
          protected abstract void deleteRecursive();
          /// &lt;summary&gt;
          ///  Copy this fileItem to the destination directory.
          /// &lt;/summary&gt;
          public abstract void copy(FileItem fileD);
          /// &lt;summary&gt;
          ///  Copy this directory and all of its elements
          /// to the destination directory.
          /// &lt;/summary&gt;
          protected abstract void copyRecursive(FileItem fileD);
          /// &lt;summary&gt;
          /// Creates a FileItem from a string path.
          /// &lt;/summary&gt;
          /// &lt;param name="path"&gt;&lt;/param&gt;
          public FileItem(String path)
          {
              Path = path;
              if (path.EndsWith("\\") || path.EndsWith("/")) IsDirectory = true;
              else IsDirectory = false;
          }
          /// &lt;summary&gt;
          /// Creates a FileItem from a FileSource directory.
          /// &lt;/summary&gt;
          /// &lt;param name="directory"&gt;&lt;/param&gt;
          public FileItem(FileSource directory)
          {
              Path = directory.Path;
          }
          public override String ToString()
          {
              return Path;
          }
          public abstract int CompareTo(object b);
      }
      /// &lt;summary&gt;
      /// A file or directory on the hard disk
      /// &lt;/summary&gt;
      public class LocalFileItem : FileItem
      {
          public override void delete()
          {
              if (!IsDirectory) File.Delete(this.Path);
              else deleteRecursive();
          }
          protected override void deleteRecursive()
          {
              Directory.Delete(Path, true);
          }
          public override void copy(FileItem destination)
          {
              if (!IsDirectory) File.Copy(destination.Path, Path, true);
              else copyRecursive(destination);
          }
          protected override void copyRecursive(FileItem destination)
          {
              Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(
                  Path, destination.Path, true);
          }
          /// &lt;summary&gt;
          /// Create's a LocalFileItem from a string path
          /// &lt;/summary&gt;
          /// &lt;param name="path"&gt;&lt;/param&gt;
          public LocalFileItem(String path)
              : base(path)
          {
          }
          /// &lt;summary&gt;
          /// Creates a LocalFileItem from a FileSource path
          /// &lt;/summary&gt;
          /// &lt;param name="path"&gt;&lt;/param&gt;
          public LocalFileItem(FileSource path)
              : base(path)
          {
          }
          public override int CompareTo(object obj)
          {
              if (obj is FileItem)
              {
                  FileItem fi = (FileItem)obj;
                  if (File.GetCreationTime(this.Path).CompareTo
                      (File.GetCreationTime(fi.Path)) &gt; 0) return 1;
                  else if (File.GetCreationTime(this.Path).CompareTo
                      (File.GetCreationTime(fi.Path)) &lt; 0) return -1;
                  else
                  {
                      if (File.GetLastWriteTime(this.Path).CompareTo
                          (File.GetLastWriteTime(fi.Path)) &lt; 0) return -1;
                      else if (File.GetLastWriteTime(this.Path).CompareTo
                          (File.GetLastWriteTime(fi.Path)) &gt; 0) return 1;
                      else return 0;
                  }
              }
              else
                  throw new ArgumentException("obj isn't a FileItem");
          }
      }
  }
  </code></pre>