开发者

getting file names depending on the selected item in combobox

开发者 https://www.devze.com 2023-04-12 09:24 出处:网络
I am thinking that this is big problem .... Ihave path like this...\"C:\\restore\\restoredb\\\" In that path i have files like this..

I am thinking that this is big problem ....

I have path like this..."C:\restore\restoredb\"

In that path i have files like this..

 restore-2011-10-12T17-16-51.zip
 restore-2011-10-11T13-24-45.zip
 restore-2011-05-11T09-45-56.zi开发者_开发技巧p
 restore-2011-08-11T09-08-07.zip
 restore-2010-09-11T09-45-12.zip 

I have a form , in that form i have a listbox and combobox(cbrestore) I have got the combobox items like this ...Month, 3 months,6 months,year...

what i want is , if i select the combobox item(month) i want to display the file names which are stored in that folder between these dates (12-10-2011 to 12-09-2011)..

If i select combobox item(3 months) i want to display the file names which are stored in that folder between these dates (12-10-2011 to 12-07-2011)..in listbox

For that i have tried this ....

 private void cbrestore_SelectedIndexChanged(object sender, EventArgs e)
 {
    string fullpathforfiles = @"C:\restore\restoredb\";
    string[] allfiles = Directory.GetFiles(fullpathforfiles);
    foreach (string single in allfiles)
    {
        string filenameonly = Path.GetFileName(single);     
    }
    if (cbrestore.SelectedValue == Daterange.type1)
    { 

    } 
}
struct Daterange 
{
    public const string type1 = "Month";
    public const string type2 = "3 Months";
    public const string type3 = "6 Months";
    public const string type4 = "Year";  

}

I dont know how to extract the exact part in that filename and adding that ... any idea how can i do this .. pls ..

Any suggestions and any code sample snippet would be very greatful to me ....

Many thanks.....


I'd do it like this:

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        List<String> t = Directory.GetFiles(@"C:\Users\justin\Desktop\New folder (2)").ToList();
        List<String> y = new List<string>();
        List<String> u = new List<string>();
        foreach (var zzz in t)
        {
            y.Add(Path.GetFileName(zzz));
        }


        if (comboBox1.Text == "Month")
        {
            u =
           (from String s in y where ((DateTime.Now.Month - DateTime.Parse(s.Substring(8, 10)).Month) < 1) && (DateTime.Now.Year - DateTime.Parse(s.Substring(8, 10)).Year == 0) select s).
               ToList();
        }
        else if (comboBox1.Text == "3 Month")
        {
            u =
           (from String s in y where ((DateTime.Now.Month - DateTime.Parse(s.Substring(8, 10)).Month) < 3) && (DateTime.Now.Year - DateTime.Parse(s.Substring(8, 10)).Year == 0) select s).
               ToList();
        }
        else if(comboBox1.Text == "1 Year")
        {
            u =
           (from String s in y where ((DateTime.Now.Month - DateTime.Parse(s.Substring(8, 10)).Month) < 12) select s).
               ToList();
        }
        
        listBox1.DataSource = u;

    }

The Result is this :

getting file names depending on the selected item in combobox

EDIT: Fixed the month problem select you see in the SS and added the year select.


I guess you could use something like below to extract the date from the string.

    filenameonly = filenameonly.Substring(filenameonly.IndexOf("-") + 1, filenameonly.IndexOf("T") - filenameonly.IndexOf("-") - 1)

Hope this helps!!


Use the code from Praveen,

filenameonly = filenameonly.Substring(filenameonly.IndexOf("-") + 1, filenameonly.IndexOf("T") -     filenameonly.IndexOf("-") - 1)

then split it into an array filenameonly.split("-")

rearange them such that you can convert to a date and check if it is within 3 months

    DateTime filetime = New DateTime();
    filetime.parse(filenameonlyarray[2] + "/" + filenameonlyarray[1] + "/" + filenameonlyarray[0]);
    if (filetime.compareto(DateTime.Now.AddMonths(-3) > 0)
    {
    //within 3 months
    }

with this date object you can now use it to check if it is within 3 months of ''

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号