Console.Write("要更新的文件\n");
ArrayList updateFiels=GetUpDateFiles(allFiles);
Console.Write("共"+updateFiels.Count+"条,时间:"+((System.TimeSpan)DateTime.Now.Subtract(startTime)).TotalSeconds+" s"+",按任意键继续\n");
Console.ReadLine();
startTime=DateTime.Now;
CopyFiles(updateFiels);
写的比较简单,只为自己用,没考虑太多.
按任意键继续,没能实现,也没有去查资料,本想实现 按Esc键 退出,按其它键继续的.如有知道请告知.
using System;
using System.IO;
using System.Collections;
namespace ArkSyncCmd


{

/**//// <summary>
/// Class1 的摘要说明。
/// </summary>
class ArkSync

{
static string fromPath;
static string toPath;
static string[] notPaths;
static ArrayList AllPaths;
static string[] notFiles;
static DateTime startTime;//运行时间
static bool showMessge=true;

/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)

{
startTime=DateTime.Now;
System.Configuration.AppSettingsReader r=new System.Configuration.AppSettingsReader();
if(args!=null&&args.Length==1&&args[0]=="0")showMessge=false;
fromPath=r.GetValue(("fromPath"),typeof(string)).ToString();
toPath=r.GetValue(("toPath"),typeof(string)).ToString();
notPaths=r.GetValue(("notPaths"),typeof(string)).ToString().Split(',');
notFiles=r.GetValue(("notFiles"),typeof(string)).ToString().Split(',');

string [] fromDir=Directory.GetDirectories(fromPath);//得到开始的目录

//Console.Write("开始目录\n");
AllPaths=new ArrayList();
AllPaths.Add(fromPath);
GetAllPaths(fromDir,notPaths);//得到所有的目录
//Console.Write("开始文件\n");
ArrayList allFiles=GetAllFiles(AllPaths,notFiles);
Console.Write("要更新的文件\n");
ArrayList updateFiels=GetUpDateFiles(allFiles);
Console.Write("共"+updateFiels.Count+"条,时间:"+((System.TimeSpan)DateTime.Now.Subtract(startTime)).TotalSeconds+" s"+",按任意键继续
\n");
Console.ReadLine();
startTime=DateTime.Now;
CopyFiles(updateFiels);
Console.Write("时间:"+((System.TimeSpan)DateTime.Now.Subtract(startTime)).TotalSeconds+" s,"+"更新完成\n");
Console.ReadLine();
}
static void CopyFiles(ArrayList updateFiles)

{
string fail="";
foreach(string path in updateFiles)

{
string top=fromPathToPath(path,fromPath,toPath);
if(!Directory.Exists(Path.GetDirectoryName(top)))Directory.CreateDirectory(Path.GetDirectoryName(top));
if(showMessge)Console.Write("正在复制:"+path+"->"+top+"\n");
try

{
FileInfo fi=new FileInfo(top);
if( fi.Attributes.ToString().IndexOf("ReadOnly")!=-1 )
fi.Attributes=FileAttributes.Normal;
File.Copy(path,top,true);
}
catch(Exception ex)

{
string ee=ex.ToString();
fail+=path+"\n";
}
}
Console.Write("失败的:\n"+fail);
}
static ArrayList GetUpDateFiles(ArrayList AllFiles)

{
DateTime datetime1;
DateTime datetime2;
ArrayList updateFiles=new ArrayList();
foreach(string file in AllFiles)

{
string top=fromPathToPath(file,fromPath,toPath);
if(!File.Exists(top))

{
updateFiles.Add(file);
if(showMessge)Console.Write(file+"-->"+top+"\n");
continue;
}
else

{
datetime1=File.GetLastWriteTime(file);
datetime2=File.GetLastWriteTime(top);
TimeSpan tsp=datetime1-datetime2;
if(Math.Abs(tsp.TotalSeconds)>1)

{
updateFiles.Add(file);
if(showMessge)Console.Write(file+"-->"+top+"\n");
}
}
}
return updateFiles;
}
static string fromPathToPath(string path,string formPath,string toPath)

{
return path.Replace(formPath,toPath);
}
static void GetAllPaths(string[] dirs,string[] notPs)

{
foreach(string dir in dirs)

{
if(StrIsIn(dir,notPs))continue;
string [] inDirs=Directory.GetDirectories(dir);
GetAllPaths(inDirs,notPs);
//Console.Write(dir+"\n");
AllPaths.Add(dir);
}
}
static ArrayList GetAllFiles(ArrayList allPaths,string []notFs)

{
ArrayList allFiels=new ArrayList();
foreach(string path in allPaths)

{
string [] files=Directory.GetFiles(path);
foreach(string file in files)

{
if(StrIsIn(file,notFs))continue;
allFiels.Add(file);
//Console.Write(file+"\n");
}
}
return allFiels;
}
static bool StrIsIn(string checkVlues,string [] notPs)

{
foreach(string str1 in notPs)
if(checkVlues.EndsWith(str1))return true;
return false;
}
}
}

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="fromPath" value="E:\ark"/><!--原始目录-->
<add key="toPath" value="E:\toark"/><!--镜像目录-->
<add key="notPaths" value="_svn,ark" /><!--不需要镜像的目录,多个以豆号分隔-->
<add key="notFiles" value="config,.cs" /><!--不需要镜像的文件,多个以豆号分隔-->
</appSettings>
</configuration>写的比较简单,只为自己用,没考虑太多.
按任意键继续,没能实现,也没有去查资料,本想实现 按Esc键 退出,按其它键继续的.如有知道请告知.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架