문제
C#으로 파일 동기화 프로그램을 만들면서 메소드를 만들어 보았습니다. copy
~에 LocalFileItem
사용하는 클래스 System.IO.File.Copy(destination.Path, Path, true)
방법 Path
는 string
.
이 코드를 대상으로 실행한 후. Path = "C:\\Test2"
그리고 this.Path = "C:\\Test\\F1.txt"
이 작업을 수행하는 데 필요한 파일 권한이 없다는 예외가 발생합니다. 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");
}
}
}
해결책
File.Copy()에서 매개변수를 잘못 배치한 것 같습니다. File.Copy(문자열 소스, 문자열 대상)여야 합니다.
또한 "C: est2"는 디렉토리입니까?파일을 디렉터리에 복사할 수 없습니다.대신 다음과 같은 것을 사용하십시오.
File.Copy( sourceFile, Path.Combine(destinationDir,Path.GetFileName(sourceFile)) );
다른 팁
나는 추측하고 있지만 다음과 같은 이유 때문일 수 있습니다.
- C:에서 파일 작업을 수행하려고 합니다.뿌리?(Vista를 사용하는 경우 Vista에서 이에 대한 보호 기능이 있을 수 있습니다. 확실하지 않습니까?)
- 존재하지 않는 디렉토리에 복사하려고 합니까?
- 파일이 이미 존재하며 잠겨 있을 수 있습니까?(즉, 다른 애플리케이션 인스턴스를 닫지 않았습니까?)
더 많은 도움을 드릴 수 없어서 죄송합니다. File.Copy에 문제가 발생한 경우는 거의 없습니다.
나는 문제를 해결할 수 있었고 Michal은 나에게 올바른 방향을 제시해주었습니다.문제는 File.Copy를 사용하여 한 위치에서 다른 위치로 파일을 복사하려고 시도한 반면 Copy 메서드는 한 파일의 모든 내용을 다른 파일로 복사하는 것(아직 존재하지 않는 경우 대상 파일 생성)이라는 것입니다.해결책은 파일 이름을 대상 디렉터리에 추가하는 것이었습니다.모든 도움에 감사드립니다!