2008-08-23 5 views
7

C#에서 파일 동기화 프로그램을 만드는 동안 copyLocalFileItem 클래스에서 이 인 System.IO.File.Copy(destination.Path, Path, true) 메서드를 사용하는 클래스를 만들려고했습니다.
이 코드를 대상으로 실행 한 후 Path = "C:\\Test2"this.Path = "C:\\Test\\F1.txt" 나는 예외 내가 C에서이 작업을 수행하는 데 필요한 파일 권한이없는 것을 말하는 얻을 : \ 테스트하지만 C : \ 테스트 자신에 (현재 사용자)가 소유하고 있습니다.
아무도 무슨 일이 일어나고 있는지 또는이 문제를 해결하는 방법을 알고 있습니까?C#의 파일 권한 정보

여기에 원본 코드가 있습니다.

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"); 
     } 
    } 
} 

답변

3

당신이 File.Copy의 매개 변수를 잘못 것 같다(), 그것은 File.Copy (문자열 소스, 문자열 대상)이어야한다.

또한 "C : \ Test2"디렉토리가 있습니까? 파일을 디렉토리에 복사 할 수 없습니다. 다음과 같이 대신 사용하십시오 :

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

0

내가 좀 여기 추측하고있어,하지만 때문에 그것은있을 수 : 루트 :

  • 당신은 C에서 파일 작업을 수행하려고? (비스타를 사용하고 있다면 비스타가 이에 대해 보호 해줄 수 있습니다 - 확실하지 않습니까?)
  • 존재하지 않는 디렉토리에 복사하려고합니까?
  • 파일이 이미 존재하며 잠길 수 있습니까? (즉, 다른 애플리케이션 인스턴스를 닫지 않은 경우)?

죄송합니다. 도움이되지 않아서 File.Copy에 거의 문제가 없었습니다.

0

나는이 문제를 해결할 수 있었고, 미칼은 올바른 방향으로 나를 지적했다. 문제는 File.Copy를 사용하여 한 위치에서 다른 위치로 파일을 복사하려고 시도하는 반면 Copy 메서드는 한 파일에서 다른 파일로 모든 내용을 복사하는 것입니다 (대상 파일이없는 경우 해당 파일을 만듭니다). 해결책은 대상 디렉토리에 파일 이름을 추가하는 것이 었습니다. 모든 도움에 감사드립니다!