复制文件时,当文件名相同但内容不同时在 C# 中生成异常
While copying file, when there is same file name but with different content is generating exception in C#
我收到异常
The process cannot access the file 'C:\Destination\New Text
Document.txt' because it is being used by another process.
我需要将所有源文件复制到目标文件夹。当 Source 和 Destination 中存在同名文件时,我需要比较 Hash 值来了解两个文件是否具有相同的内容。如果找到相同的内容我需要跳过复制过程但是如果内容不同我需要复制文件。
我的class结构如下:
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)
{
}
}
}
}
Filestream 是一次性类型。
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);
}
我收到异常
The process cannot access the file 'C:\Destination\New Text Document.txt' because it is being used by another process.
我需要将所有源文件复制到目标文件夹。当 Source 和 Destination 中存在同名文件时,我需要比较 Hash 值来了解两个文件是否具有相同的内容。如果找到相同的内容我需要跳过复制过程但是如果内容不同我需要复制文件。
我的class结构如下:
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)
{
}
}
}
}
Filestream 是一次性类型。
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);
}