在C#中创建文件同步程序时,我尝试在LocalFileItem类中创建一个方法copy ,该类使用System.IO.File.Copy(destination.Path, Path, true)方法,其中Path是一个string
用目标执行此代码后。 Path = "C:\\\\Test2"this.Path = "C:\\\\Test\\\\F1.txt"我得到一个例外,说我没有在C:\\ Test上执行此操作所需的文件权限,但C:\\ Test由我自己(当前用户)拥有
有谁知道发生了什么,或者如何解决这个问题?

这是完整的原始代码。

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Diones.Util.IO
{
    /// <summary>
    /// An object representation of a file or directory.
    /// </summary>
    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; }
        }
        /// <summary>
        ///  Delete this fileItem.
        /// </summary>
        public abstract void delete();
        /// <summary>
        ///  Delete this directory and all of its elements.
        /// </summary>
        protected abstract void deleteRecursive();
        /// <summary>
        ///  Copy this fileItem to the destination directory.
        /// </summary>
        public abstract void copy(FileItem fileD);
        /// <summary>
        ///  Copy this directory and all of its elements
        /// to the destination directory.
        /// </summary>
        protected abstract void copyRecursive(FileItem fileD);
        /// <summary>
        /// Creates a FileItem from a string path.
        /// </summary>
        /// <param name="path"></param>
        public FileItem(String path)
        {
            Path = path;
            if (path.EndsWith("\\") || path.EndsWith("/")) IsDirectory = true;
            else IsDirectory = false;
        }
        /// <summary>
        /// Creates a FileItem from a FileSource directory.
        /// </summary>
        /// <param name="directory"></param>
        public FileItem(FileSource directory)
        {
            Path = directory.Path;
        }
        public override String ToString()
        {
            return Path;
        }
        public abstract int CompareTo(object b);
    }
    /// <summary>
    /// A file or directory on the hard disk
    /// </summary>
    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);
        }
        /// <summary>
        /// Create's a LocalFileItem from a string path
        /// </summary>
        /// <param name="path"></param>
        public LocalFileItem(String path)
            : base(path)
        {
        }
        /// <summary>
        /// Creates a LocalFileItem from a FileSource path
        /// </summary>
        /// <param name="path"></param>
        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)) > 0) return 1;
                else if (File.GetCreationTime(this.Path).CompareTo
                    (File.GetCreationTime(fi.Path)) < 0) return -1;
                else
                {
                    if (File.GetLastWriteTime(this.Path).CompareTo
                        (File.GetLastWriteTime(fi.Path)) < 0) return -1;
                    else if (File.GetLastWriteTime(this.Path).CompareTo
                        (File.GetLastWriteTime(fi.Path)) > 0) return 1;
                    else return 0;
                }
            }
            else
                throw new ArgumentException("obj isn't a FileItem");
        }
    }
}

===============>>#1 票数:4 已采纳

看来你错放了File.Copy()中的参数,它应该是File.Copy(字符串源,字符串目标)。

还是“C:\\ Test2”一个目录? 您无法将文件复制到目录。 使用类似的东西:

File.Copy( 
    sourceFile,
    Path.Combine(destinationDir,Path.GetFileName(sourceFile))
    )
;

===============>>#2 票数:0

我有点猜测,但可能是因为:

  • 您正在尝试在C:root中执行文件操作? (如果你使用它可能会有Vista的保护 - 不确定?)
  • 您是否正在尝试复制到不存在的目录?
  • 该文件已存在并可能被锁定? (即你还没有关闭另一个应用程序实例)?

对不起,我无法提供更多帮助,我很少遇到File.Copy的问题。

===============>>#3 票数:0

我能够解决问题,Michal指出了正确的方向。 问题是我尝试使用File.Copy将文件从一个位置复制到另一个位置,而Copy方法只将所有内容从一个文件复制到另一个文件(如果目标文件尚不存在,则创建目标文件)。 解决方案是将文件名附加到目标目录。 感谢您的帮助!

  ask by Diones translate from so

未解决问题?本站智能推荐: