Click here to Skip to main content
15,896,730 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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");
        }
Posted
Updated 20-Feb-17 5:26am

1 solution

Try this:

C#
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);
}


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)[^]
 
Share this answer
 
v2
Comments
Garth J Lancaster 20-Feb-17 18:10pm    
sweet (similar to the 'pattern' I use for such) +5
Maciej Los 21-Feb-17 2:06am    
Thank you, Garth.
Cheers
Maciej
Member 12558924 21-Feb-17 2:21am    
Hey,
Many thanks!!! It works for the zip file, but is it possible to leave the choice to the user to enter his own date range whenever he needs it using the DateTimePicker Control on my Forms(Interface)? In addition I also want to be able to enter the exact time of the chosen date. That what i've tried does not work.
Any ideas? Thank you in advance.
Maciej Los 21-Feb-17 2:42am    
Yes, you can do that. All you need to to is to set up startdate and enddate, for example:
DateTime startdate = DateTimePicker1.Value;

Please, accept an answer as a solution (green button).
Member 12558924 21-Feb-17 2:59am    
It's done! should l made it in the code behind of the DateTimePicker Control or in the Button Control where i write all this code? I made it in the code behind of the DateTimePicker Control but i'm not sure. Thanks a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900