c# - C# 复制具有指定日期和时间范围的文件
问题描述
在 Visual C# 中:
我希望将指定日期和时间范围的文件列表从一个文件夹复制到另一个文件夹。我不断获取所有文件,而不仅仅是我想要的文件。
例如:
2019 年 2 月 20 日凌晨 2 点至 2019 年 3 月 2 日凌晨 1 点(基于日期时间修改)
复制
D:\Data\SubFolder1\SubFolder2\SubFolder3\\*.log
至
E:\MyLogs\D\Data\SubFolder1\SubFolder2\SubFolder3\
我应该看什么函数或库?
解决方案
您可以尝试如下代码
从中导入System.IO
使用DirectoryInfo
。
我也正在从中导入System.Linq
使用Where
方法。
假设您将目录路径放在变量中说yourDirectoryPath
// Specify the directory you want to use
DirectoryInfo directory = new DirectoryInfo(yourDirectoryPath);
// Check if your directory exists and only then proceed further
if (directory.Exists){
//You would be having your fromdate and toDate in two variables like fromDate, toDate
// files variable below will have all the files that has been lastWritten between the given range
var files = directory.GetFiles()
.Where(file=>file.LastWriteTime >= fromDate && file.LastWriteTime <= toDate);
}
现在您可以使用您现有的代码(如果您没有,请告诉我)将文件夹中的所有文件复制到目标位置。
推荐阅读
- linux - SQLite Studio 键盘输入损坏
- c# - 转换器不能应用于需要 IValueConverter 类型的属性
- django - 如何使用 zip() 函数插入数据
- r - 使用一列作为开始,另一列作为框的停止绘制图
- jenkins - currentBuild.changeSets 在管道 Jenkins 中通过提交剪切消息
- javafx - 为什么我的标签显示“String called name”Null
- javascript - 为什么 window.onload 只能在第一个数组值上按预期工作,而我必须重新加载页面以获取其他数组值?
- python - 与 groupby 和 mean 结合使用时,Fillna 不起作用
- dart - Flutter CustomScrollView 和 Scoped 模型问题
- jpa - 将 EJB 2 CMP 实体 bean 迁移到 EJB 3 JPA 实体时如何处理 FinderException?