<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>

[{'answer_id': 24274, 'author': 'Michał Piaskowski', 'author_id': 1534, 'author_profile': 'https://Stackoverflow.com/users/1534', 'pm_score': 3, 'selected': True, 'text': '<p>It seems you have misplaced the parameters in File.Copy(), it should be  File.Copy(string source, string destination).</p>\n\n<p>Also is "C:\\Test2" a directory? You can\'t copy file to a directory. \nUse something like that instead:\n<pre>\nFile.Copy( \n    sourceFile,\n    Path.Combine(destinationDir,Path.GetFileName(sourceFile))\n    )</pre>;</p>\n'}, {'answer_id': 24276, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 0, 'selected': False, 'text': "<p>I'm kinda guessing here, but could it be because:</p>\n\n<ul>\n<li>You are trying to perform file operations in C: root? (there may be protection on this by Vista if you are using it - not sure?)</li>\n<li>You are trying to copy to a non-existant directory?</li>\n<li>The file already exists and may be locked? (i.e you have not closed another application instance)?</li>\n</ul>\n\n<p>Sorry I cant be of more help, I have rarely experienced problems with File.Copy.</p>\n"}, {'answer_id': 24790, 'author': 'Diones', 'author_id': 2605, 'author_profile': 'https://Stackoverflow.com/users/2605', 'pm_score': 0, 'selected': False, 'text': '<p>I was able to solve the problem, Michal pointed me to the right direction.\nThe problem was that I tried to use File.Copy to copy a file from one location to another, while the Copy method does only copy all the contents from one file to another(creating the destination file if it does not already exists). The solution was to append the file name to the destination directory.\nThanks for all the help!</p>\n'}]
['https://Stackoverflow.com/questions/24262', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2605/']