2008-08-23 25 views
7

Ben Path bir string olduğunu System.IO.File.Copy(destination.Path, Path, true) yöntemini kullanır LocalFileItem sınıfında bir yöntem copy yapmaya çalıştı C# bir dosya senkronizasyonu programı oluştururken.
Bu kodu hedef ile yürüttükten sonra. Path = "C:\\Test2" ve this.Path = "C:\\Test\\F1.txt" ben bir istisna ben C Bu işlemi yapmak için gerekli dosya izinlerini sahip olmadığını söyleyerek olsun: \ Testi ancak C: \ Test kendim (geçerli kullanıcı) aittir.
Neler olup bittiğini veya bunun nasıl geçeceğini bilen var mı?Hakkında Dosya izinleri

İşte orjinal kod tamamlandı.

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

cevap

3

Size File.Copy parametreleri kaybettiyseniz görünüyor(), bu File.Copy (string kaynak, dize hedef) olmalıdır.

Ayrıca "C: \ Test2" bir dizindir? Dosyayı bir dizine kopyalayamazsınız. Bunun gibi bir şey kullanın:

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

0

Ben biraz burada tahmin ediyorum, ama çünkü bu olabilir: root:

  • Sen C dosya işlemlerini gerçekleştirmek için çalışıyoruz? (Eğer bunu kullanıyorsanız Vista tarafından bu koruma olabilir - emin değil misiniz?)
  • Varolmayan bir dizine kopyalamaya çalışıyorsunuz?
  • Dosya zaten mevcut ve kilitli olabilir mi? (yani başka bir uygulama örneğini kapatmadınız)?

Üzgünüz, daha fazla yardım edemiyorum, File.Copy ile ilgili sorun yaşamadım.

0

Sorunu çözebildim, Michal doğru yöne işaret etti. Sorun, bir dosyayı bir konumdan diğerine kopyalamak için File.Copy kullanmayı denediğimde Kopyalama yöntemi tüm içeriği yalnızca bir dosyadan diğerine kopyalar (zaten mevcut değilse hedef dosyayı oluşturur). Çözüm, dosya adını hedef dizine eklemekti. Yardım için teşekkürler!