CopyUSB/FileCopy.cs

191 lines
5.0 KiB
C#

using CopyUSB.MyDB;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CopyUSB;
public class CopyList
{
public string Func { get; set; }
public long length { get; set; }
public FileInfo file { get; set; }
public string des { get; set; }
}
public class FileCopy
{
public static void Copy(string source,string des)
{
try
{
var sourceDir = new DirectoryInfo(source);
var desDir = new DirectoryInfo(des);
// 确保目标目录存在
if (!desDir.Exists)
{
desDir.Create();
}
using(var fileInfoDb = new FileInfoDb())
{
foreach (var file in sourceDir.GetFiles())
{
try
{
string destinationFile = Path.Combine(desDir.FullName, file.Name);
var tryGetFileDbInfo = fileInfoDb.Files.Where(w => w.Path == destinationFile).FirstOrDefault();
if (tryGetFileDbInfo != null)
{
if (tryGetFileDbInfo.Size == file.Length)
{
Debug.WriteLine($"Find the Same File In Db {destinationFile}");
continue;
}
else
{
tryGetFileDbInfo.NeedChange = true;
tryGetFileDbInfo.Size = file.Length;
file.CopyTo(destinationFile, true);
Debug.WriteLine($"renew {destinationFile} from Db");
}
}
else
{
tryGetFileDbInfo = new MyDB.Class.FileInfoTable();
tryGetFileDbInfo.Path = destinationFile;
tryGetFileDbInfo.Size = file.Length;
fileInfoDb.Files.Add(tryGetFileDbInfo);
file.CopyTo(destinationFile, true);
Debug.WriteLine($"new {destinationFile} into Db and Copy");
}
/*if (File.Exists(destinationFile))
{
var desFileInfo = new FileInfo(destinationFile);
if (desFileInfo.Length != file.Length)
{
*//*Console.WriteLine($"Renew {file} --> {destinationFile}");*//*
copyList.Add((file.Length, file, destinationFile, "Renew"));
*//*file.CopyTo(destinationFile, true);*//*
}
else
{
Debug.WriteLine($"Same {file} --> {destinationFile} {file.Length / 1024 / 1024}MB");
*//*copyList.Add((file, destinationFile, 0, "Same"));*//*
}
}
else
{
*//*Console.WriteLine($"NewFile {file} --> {destinationFile}");*//*
copyList.Add((file.Length, file, destinationFile, "NewFile"));
}*/
}catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
fileInfoDb.SaveChanges();
}
//遍历目录
foreach (var subDir in sourceDir.GetDirectories())
{
Copy(subDir.FullName, Path.Combine(desDir.FullName, subDir.Name));
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
/*Console.WriteLine($"Copy Error {source}");*/
/*Console.WriteLine(ex.ToString());*/
}
}
public static long DicFileNum(string source)
{
long num = 0;
using (var fileInfoDb = new FileInfoDb())
{
num = fileInfoDb.Files.Where(w=>w.NeedChange==true).Count();
/*FileNum(source,ref num);*/
Debug.WriteLine($"Totle Dic Num is {num} (From Db)");
}
return num<0?0:num;
}
private static void FileNum(string source, ref long num)
{
try
{
var sourceDir = new DirectoryInfo(source);
num += sourceDir.GetFiles().Length;
//遍历目录
foreach (var subDir in sourceDir.GetDirectories())
{
FileNum(subDir.FullName,ref num);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
/*Console.WriteLine($"Copy Error {source}");*/
/*Console.WriteLine(ex.ToString());*/
}
}
public static void SimpleCopy(string des,string baseDic , ref Int64 doneNum)
{
/*try
{*/
var desDir = new DirectoryInfo(des);
//数据库查找需要更改的数据
using (var fileInfoDb = new FileInfoDb())
{
var allNeedChangeFile = fileInfoDb.Files.Where(w => w.NeedChange == true).OrderBy(o => o.Size).ToList();
foreach (var file in allNeedChangeFile)
{
var desInfo = new FileInfo(Path.Join(des, file.Path.Replace(baseDic, "")));
var fileInfo = new FileInfo(file.Path);
if (!fileInfo.Exists)
{
Debug.WriteLine($"Cannot Find File In Computer {fileInfo.FullName}");
file.NeedChange= false;
continue;
}
if (!desInfo.Directory.Exists)
{
try
{
desInfo.Directory.Create();
}catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
try
{
fileInfo.CopyTo(desInfo.FullName, true);
fileInfo.Delete();
file.NeedChange = false;
Debug.WriteLine($"moved the file and marked into Db {fileInfo.FullName} --> {desInfo.FullName}");
}
catch(Exception ex)
{
Debug.WriteLine($"Error: moved the file and marked into Db {fileInfo.FullName} --> {desInfo.FullName}\n{ex.ToString()}");
}
doneNum++;
}
fileInfoDb.SaveChanges();
}
/*}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
*//*Console.WriteLine($"Copy Error {source}");*/
/*Console.WriteLine(ex.ToString());*//*
}*/
}
}