c# 新手 ,请问怎么找出一个文件夹中内容变动过的文件,复制到另一个文件夹 c# 新手 ,请问怎么找出一个文件夹中内容变动过的文件,复制到另一个文件夹 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 参考http://www.cnblogs.com/springyangwc/archive/2011/08/27/2155547.html FileInfo.CreationTime 文件的创建时间FileInfo.LastWriteTime 文件的上次写操作时间 两者不同,可视为被改动了 也可以先过去文件md5,发现变动后用新md5再次验证。 你好,这样写对不对? private void CopyDifFile(string srcPath, string aimPath) { DirectoryInfo directory = new DirectoryInfo(srcPath); var files = directory.GetFiles() .Where(file => file.LastWriteTime != file.CreationTime); string s = ""; foreach (FileInfo file in files) { s = file.Name; if (s != "") { MessageBox.Show("文件已存在"); break; //CopyDir(srcPath + s, aimPath + Path.GetFileName(s)); } // 否则直接Copy文件 else { MessageBox.Show("文件" + s + "更改"); System.IO.File.Copy(srcPath + Path.GetFileName(s), aimPath + Path.GetFileName(s), true); } } } 监测文件变化可以使用FileSystemWatcher使用方法参考这个或者自行搜索https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx因为涉及IO操作,所以相关文件夹的权限设置是少不了的 想找出srcpath 中所有变动的文件 复制到 aimpath 中,上面代码有错误,不知道怎么改,求帮助 这样用 var p = new FileInfo("1.txt"); Console.WriteLine(p.CreationTime); Console.WriteLine(p.LastWriteTime); private void CopyDifFile(string srcPath, string aimPath) { DirectoryInfo directory = new DirectoryInfo(srcPath); var files = directory.GetFiles() .Where(file => file.LastWriteTime != file.CreationTime); //FileInfo file = new FileInfo(srcPath); string s = ""; //fi.CopyTo(aimPath, true); foreach (FileInfo file in files) { s = file.Name; if (s != "") { MessageBox.Show("文件已存在"); break; //file.CopyTo(aimPath,true); //CopyDir(srcPath + s, aimPath + Path.GetFileName(s)); } // 否则直接Copy文件 else { MessageBox.Show("文件" + s + "更改"); System.IO.File.Copy(srcPath + Path.GetFileName(s), aimPath + Path.GetFileName(s), true); } } } 这个可以用FileSystemWatcher watcher = new FileSystemWatcher();watcher.Pathwatcher.Changed += Watcher_Changed;private void Watcher_Changed(object sender, FileSystemEventArgs e) { //这里写操作方法不就行了吗 }FileSystemWatcher watcher = new FileSystemWatcher(); 好像文件的增删查改都可以的,然后在把这个程序做成window services,就省心了FileSystemWatcher curWatcher = new FileSystemWatcher(); curWatcher.BeginInit(); curWatcher.IncludeSubdirectories = true; //curWatcher.Path = ConfigurationManager.AppSettings["FileMonitorDirectory"]; curWatcher.Path = "D:\\images"; curWatcher.Changed += new FileSystemEventHandler(OnFileChanged); curWatcher.Created += new FileSystemEventHandler(OnFileCreated); curWatcher.Deleted += new FileSystemEventHandler(OnFileDeleted); curWatcher.Renamed += new RenamedEventHandler(OnFileRenamed); curWatcher.EnableRaisingEvents = true; curWatcher.EndInit(); 使用文件监视就可以了:FileSystemWatcher ASP.NET 的 Gridview 如何做到输入单元格的值,自动计算另外一列的值 求助,为何我的程序无法自动运行关闭系统服务的指令 别人的程序里经常出现marshall,这个单词什么意思? 去掉XML格式 Pointer.Box使用问题 一个人开发一个C#ERP,求各位大神指点用什么框架和无刷新技术 请教Response.AppendHeader 容器问题! 请教一个简单问题,C#中如何使用 pen 画一条直线?! C# 自学书籍上无法执行的一段程序,求修改 求Brooks的WinSECS组件
FileInfo.LastWriteTime 文件的上次写操作时间
两者不同,可视为被改动了
你好,这样写对不对? private void CopyDifFile(string srcPath, string aimPath)
{
DirectoryInfo directory = new DirectoryInfo(srcPath);
var files = directory.GetFiles()
.Where(file => file.LastWriteTime != file.CreationTime);
string s = "";
foreach (FileInfo file in files)
{
s = file.Name;
if (s != "")
{
MessageBox.Show("文件已存在");
break;
//CopyDir(srcPath + s, aimPath + Path.GetFileName(s));
}
// 否则直接Copy文件
else
{
MessageBox.Show("文件" + s + "更改");
System.IO.File.Copy(srcPath + Path.GetFileName(s), aimPath + Path.GetFileName(s), true);
}
}
}
使用方法参考这个或者自行搜索
https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
因为涉及IO操作,所以相关文件夹的权限设置是少不了的
var p = new FileInfo("1.txt");
Console.WriteLine(p.CreationTime);
Console.WriteLine(p.LastWriteTime);
{
DirectoryInfo directory = new DirectoryInfo(srcPath);
var files = directory.GetFiles()
.Where(file => file.LastWriteTime != file.CreationTime);
//FileInfo file = new FileInfo(srcPath);
string s = "";
//fi.CopyTo(aimPath, true);
foreach (FileInfo file in files)
{
s = file.Name;
if (s != "")
{
MessageBox.Show("文件已存在");
break;
//file.CopyTo(aimPath,true);
//CopyDir(srcPath + s, aimPath + Path.GetFileName(s));
}
// 否则直接Copy文件
else
{
MessageBox.Show("文件" + s + "更改");
System.IO.File.Copy(srcPath + Path.GetFileName(s), aimPath + Path.GetFileName(s), true);
}
}
}
watcher.Path
watcher.Changed += Watcher_Changed;
private void Watcher_Changed(object sender, FileSystemEventArgs e)
{
//这里写操作方法不就行了吗
}FileSystemWatcher watcher = new FileSystemWatcher();
curWatcher.BeginInit();
curWatcher.IncludeSubdirectories = true;
//curWatcher.Path = ConfigurationManager.AppSettings["FileMonitorDirectory"];
curWatcher.Path = "D:\\images"; curWatcher.Changed += new FileSystemEventHandler(OnFileChanged); curWatcher.Created += new FileSystemEventHandler(OnFileCreated); curWatcher.Deleted += new FileSystemEventHandler(OnFileDeleted); curWatcher.Renamed += new RenamedEventHandler(OnFileRenamed); curWatcher.EnableRaisingEvents = true; curWatcher.EndInit();