正在获取两个日期之间的文件
本文关键字:日期 两个 之间 文件 获取 | 更新日期: 2023-09-27 18:27:43
如何列出两个日期之间的文件。现在它遍历文件夹中的每个文件并返回所有文件。
protected void btnListItems_Click(object sender, EventArgs e)
{
lblMessage.Visible = false;
//lblEnddatse.Visible = true;
Boolean status = true;
Util objUtil = new Util();
String Message = "";
DateTime SDate = new DateTime();
DateTime EDate = new DateTime();
string str = "";
DataTable tbl = new DataTable();
DataTable dt = new DataTable();
DataRow dr;
String[] s1;
//DirectoryInfo d = new DirectoryInfo();
s1 = Directory.GetFiles(@"C:/PCRequestFiles");
for (int i = 0; i <= s1.Length - 1; i++)
{
if (i == 0)
{
//Add Data Grid Columns with name
dt.Columns.Add("FileName");
dt.Columns.Add("GeneratedTime");
}
//Get each file information
FileInfo f = new FileInfo(s1[i]);
FileSystemInfo f1 = new FileInfo(s1[i]);
dr = dt.NewRow();
//Get File name of each file name
dr["FileName"] = f1.Name;
dr["GeneratedTime"] = f1.CreationTime.Date.ToString("dd/MM/yyyy");
string a = f1.CreationTime.Date.ToString("dd/MM/yyyy");
//Insert collected file details in Datatable
string fromdate = txtFromDate.Text.ToString();
string todate = txtToDate.Text.ToString();
dt.Rows.Add(dr);
if ((f.Length / 1024) > 5000)
{
lblMessage .Text = "" + f1.Name + " had reach its size limit.";
}
else
{ }
}
if (dt.Rows.Count > 0)
{
gvFileGenStatus.DataSource = dt;
gvFileGenStatus.DataBind();
}
}
我试着给出条件,但不起作用。是否仍然可以使用日期过滤器列出目录中的文件名。感谢您提前提供的帮助)。
我相信这会有所帮助:
DirectoryInfo DirInfo = new DirectoryInfo(@"C:/PCRequestFiles");
var files = from f in DirInfo.EnumerateFiles()
where f.CreationTimeUtc < EndDate && f.CreationTimeUtc > StartDate
select f;
您可以使用以下代码获取在指定开始日期和指定结束日期之间创建的文件(在目录及其子目录中)。
DateTime endTime = DateTime.Now;
DateTime starttime = endTime.AddDays(-2);
string searchPattern="*.*";
var filesBetweenDates = Directory.GetFiles("Path to your directory", searchPattern,SearchOption.AllDirectories).Where
(f => new FileInfo(f).CreationTime > starttime && new FileInfo(f).CreationTime < endTime);
您可以执行类似的操作
foreach (FileInfo flInfo in directory.GetFiles())
{
DateTime yesterday = DateTime.Today.AddDays(-1);
String name = flInfo.Name.Substring(3,4);
DateTime creationTime = flInfo.CreationTime;
if (creationTime.Date == yesterday.Date)
yesterdaysList.Add(name);
}