2017-04-19 13 views
0

で例外を発生していると私は例外が同じファイル名であるが、異なるコンテンツは、C#

プロセスはファイル「Cにアクセスすることはできませんを取得していたときに、ファイルのコピー中:\先新しいテキスト ドキュメント\。 txt 'は別のプロセスで使用されているためです。

すべてのソースファイルをコピー先のフォルダにコピーする必要があります。ソースとデスティネーションに同じ名前のファイルがある場合、両方のファイルが同じ内容であるかどうかを知るためにハッシュ値を比較する必要があります。同じコンテンツが見つかった場合はコピー処理をスキップする必要がありますが、コンテンツが異なる場合はファイルをコピーする必要があります。

私のクラス構造は以下の通りです:

using System; 
using System.Configuration; 
using System.IO; 
using System.Linq; 
using System.Security.Cryptography; 
using System.Text; 

namespace TestBackups 
{ 
    public class FilesBackup 
    { 
     private string LookupDirectory; 
     private string BackupDirectory; 
     public FilesBackup() 
     { 
      LookupDirectory = "C:\\Source"; 
      BackupDirectory = "C:\\Destination"; 
     } 

     public void PerformBackup() 
     { 
      DirSearch(LookupDirectory);   
     } 

     public void DirSearch(string dir) 
     { 
      foreach (string file in Directory.GetFiles(dir)) 
      { 
       // Console.WriteLine(string.Format("Filename : {0} and Lenghth : {1}", file, file.Length)); 
       string destFilePath = file.Replace(LookupDirectory, BackupDirectory); 

       if (!File.Exists(file.Replace(LookupDirectory, BackupDirectory))) 
       { 
        Console.WriteLine("DO Not Exists - copy file"); 
        File.Copy(file, destFilePath); 
       } 
       else 
       { 
        Console.WriteLine("Exists - check for the same content"); 

        FileInfo sourceFile = new FileInfo(file); 
        FileInfo destinationFile = new FileInfo(destFilePath); 

        if (FilesAreEqual_Hash(sourceFile, destinationFile)) 
        { 
         //Skip -Don't copy file 
        } 
        else 
        { 
         // Override the existing file       
         File.Copy(file, destFilePath, true); 
        } 
       } 
      } 

      foreach (string d in Directory.GetDirectories(dir)) 
      { 
       Console.WriteLine(d); 
       DirSearch(d); 
      } 
     } 

     public bool FilesAreEqual_Hash(FileInfo first, FileInfo second) 
     { 
      byte[] firstHash = MD5.Create().ComputeHash(first.OpenRead()); 
      byte[] secondHash = MD5.Create().ComputeHash(second.OpenRead()); 

      for (int i = 0; i < firstHash.Length; i++) 
      { 
       if (firstHash[i] != secondHash[i]) 
        return false; 
      } 
      return true; 
     } 

    } 
} 

コードは、ファイルを働いているが、先の同じ名前のファイルがある場合もございますが内部のさまざまなコンテンツと、例外を発生させます。

マイコンソールコードファイルストリームは、使い捨てタイプである

using System; 

namespace TestBackups 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      try 
      { 

       FilesBackup backup= new FilesBackup();       
       backup.PerformBackup();  
       Console.ReadLine(); 
      } 
      catch (Exception ex) 
      { 

      } 
     } 
    } 
} 
+0

プット日:A19Apr2017を例 –

+0

として、私は他の { //既存のファイル File.Copyを上書きでエラーを取得していますが(ファイル、destFilePath、真); } ファイル名を変更できません。宛先でコンテンツが変更された場合は、ファイルを上書きする必要があります。 – Simant

+0

ok ..あなたはそれを削除してリメイクすることができます。 –

答えて

2

です。

byte[] firstHash = MD5.Create().ComputeHash(first.OpenRead()); 
byte[] secondHash = MD5.Create().ComputeHash(second.OpenRead()); 

このような何か試してみてください:

byte[] firstHash, secondHash; 
using (FileStream fs = File.OpenRead(first)) 
{ 
    firstHash = MD5.Create().ComputeHash(fs); 
} 
using (FileStream fs = File.OpenRead(second)) 
{ 
    secondHash = MD5.Create().ComputeHash(fs); 
} 
各ファイルエンドなどで
関連する問題