170 lines
4.5 KiB
C#
170 lines
4.5 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())
|
|
{
|
|
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} from Db");
|
|
}
|
|
/*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"));
|
|
}*/
|
|
|
|
}
|
|
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}");
|
|
}
|
|
return 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($"暂存位置文件消失 {fileInfo.FullName}");
|
|
continue;
|
|
}
|
|
if (!desInfo.Directory.Exists)
|
|
{
|
|
desInfo.Directory.Create();
|
|
}
|
|
fileInfo.CopyTo(desInfo.FullName,true);
|
|
fileInfo.Delete();
|
|
file.NeedChange = false;
|
|
Debug.WriteLine($"moved the file and marked {fileInfo.FullName} --> {desInfo.FullName}");
|
|
doneNum++;
|
|
}
|
|
fileInfoDb.SaveChanges();
|
|
}
|
|
/*}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine(ex.ToString());
|
|
*//*Console.WriteLine($"Copy Error {source}");*/
|
|
/*Console.WriteLine(ex.ToString());*//*
|
|
}*/
|
|
}
|
|
}
|