如何在文件夹的日期范围之间检查和获取zip文件,并使用C#winforms将其写入组合框 [英] How to check and get zip files between date range from a folder and write it into combobox using C# winforms
问题描述
你好!!!
我需要一些C#winforms的帮助。我首先想要使用Datetimepicker来设置日期范围,在该日期范围内我可以每周检查文件夹上是否有新的zip文件。如果是这样的话我想在我的组合框中获得这个新的zipfile(只有文件名)。之后我想在datagridview上获取zip文件的所有条目。
我该怎么做?有人可以帮我提出任何想法吗?
非常感谢!!!
我是什么尝试过:
Hello!!!
i need some help in C# winforms. I firstly want to use a Datetimepicker to set the date range in which l could weekly check if there is a new zip file on the folder. if that's the case i want to get this new zipfile (only the filename) in my combobox. And after i'm suppose to get all entries of the zip file on a datagridview.
How can i do this? can someone help me with any ideas?
Many thanks!!!
What I have tried:
// Show the OpenFileDialog.
DialogResult result = ofd.ShowDialog();
if (result == DialogResult.OK)
{
string fullPath = ofd.FileName;
string directory;
directory = fullPath.Substring(0, fullPath.LastIndexOf('\\'));
textBox1.Text = directory;
DateTime.Now.ToString("yyyyMMdd"); // ??? i don't know how i can query the date range so that it will be considered in my code
if (File.Exists(fullPath))
{
string[] AllZipFiles = Directory.GetFiles(directory,"*.zip",SearchOption.AllDirectories); // Get all files from the folder
foreach (string zipfile in AllZipFiles)
{
this.comboBox1.Items.Add(zipfile); // it' working but with all the fullpath, but i only want to get the filename like test.zip
}
}
else
MessageBox.Show("There is not Zip File in this Folder");
}
推荐答案
试试这个:
Try this:
string path = @"D:\Audiobooks"; //change initial path to your needs
string filter = "*.zip";
DateTime startdate = new DateTime(2012,1,1); //change dates to your needs
DateTime enddate = new DateTime(2016,12,31);
List<string> files = Directory.EnumerateFiles(path, filter, SearchOption.AllDirectories)
.Where(x=>File.GetLastWriteTime(x)>=startdate && File.GetLastWriteTime(x)<=enddate)
.Select(x=>Path.GetFileName(x))
.ToList();
foreach(string zipfile in files)
{
combobox1.Items.Add(zipfile);
}
详情请见:
Directory.EnumerateFiles方法(String,String,SearchOption)(System.IO) [ ^ ]
File.GetLastWriteTime Method(String )(System.IO) [ ^ ]
Path.GetFileName Method(String)(System.IO) [ ^ ]
For further details, please see:
Directory.EnumerateFiles Method (String, String, SearchOption) (System.IO)[^]
File.GetLastWriteTime Method (String) (System.IO)[^]
Path.GetFileName Method (String) (System.IO)[^]
这篇关于如何在文件夹的日期范围之间检查和获取zip文件,并使用C#winforms将其写入组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!