-2

I create a web site that helps user to retrieve file from direction D drive depend on creation date and view the files in list box , If user enter the date in text box the list will show only the files which are same as user entered date.I don't get any error but when I write if (TextBox1.Text.Equals(dt.ToString("dd/MM/yyyy"))) this code line no file display on list box however I want to get files based on compare between user entre date and get creation file date .

protected void Button1_Click(object sender, EventArgs e)
{

    DirectoryInfo dinfo = new DirectoryInfo(@"D:\Local_temp");
    FileInfo[] files = dinfo.GetFiles("*.MSG");

    DateTime dt;

    foreach (FileInfo file in files)
    {

        dt = File.GetCreationTime(file.ToString());

        if (TextBox1.Text.Equals(dt.ToString("dd/MM/yyyy")))
            // DateTime dt = file.CreationTime;
            ListBox1.Items.Add(file.Name);
    }
}
Mahra
  • 5
  • 3
  • 1
    And the error or program behavior you're seeing is.....? And if you respond, please do not say "It doesn't work." Either describe the behavior or tell us what error you are seeing. – Tim May 11 '14 at 08:42
  • Welcome to SO. It's unclear, what u r asking. You describe, what your function should do, but don't say what it actually does and what's missing. – Christian St. May 11 '14 at 08:51
  • what's the issue you are facing? and what's this line returns as value file.ToString() – qamar May 11 '14 at 09:03
  • 2
    I edit the question please reread it – Mahra May 11 '14 at 09:10

2 Answers2

0

A better approach for entering date would be to use a DateTimePicker user control (for web for example jQueryUI). Don't use custom formats to compare DateTime objects. In your case you need to parse the entered String to a DateTime object and then compare it. You can do this by using the DateTime.TryParse method:

// user entered 01.May
String date = "2014-05-01 12:01:01"; // TextBox1.Text
DateTime timestamp = DateTime.Now;
// parse the entered string
if (!DateTime.TryParse(date, out timestamp))
{
    throw new FormatException("Entered Date/Time has an invalid format");
}
// take some files for the example
var files = new DirectoryInfo("d:\\temp\\xml\\").GetFiles().ToList();
// show all files
files.ForEach(file => Console.WriteLine("{0} - {1}", file.CreationTime, file.Name));
Console.WriteLine("Filtered");
// filter files from May
foreach (var file in files)
{
    if (file.CreationTime >= timestamp)
    {
        Console.WriteLine("{0} - {1}", file.CreationTime, file.Name);
    }
}

In your case it should work if you use the FileInfo.CreationTime property compared with the desired timestamp, created as a DateTime object. For my example i have 3 files, one of them is created in May 2014 and i want to show only files that are created after 01.May (01.05.2014). The output is:

29.01.2014 19:24:51 - 1.xml
29.01.2014 19:25:20 - 2.xml
03.05.2014 19:32:04 - xml.xml
Filtered
03.05.2014 19:32:04 - xml.xml

Have a look at this SO answer - How to compare DateTime for another possibility to compare / work with DateTime objects.

Community
  • 1
  • 1
keenthinker
  • 7,645
  • 2
  • 35
  • 45
0

Try this, also do not forget to validate your input, also I recommend you to use a DateTime picker instead of textbox

protected void Button1_Click(object sender, EventArgs e)
{
    DirectoryInfo dinfo = new DirectoryInfo(@"D:\Local_temp");
    FileInfo[] files = dinfo.GetFiles("*.msg");

    DateTime dt;
    if (DateTime.TryParse(this.TextBox1.Text, out dt))
    {
        files.Where(x => File.GetCreationTime(x.FullName).Date == dt.Date).ToList().ForEach(x => this.ListBox1.Items.Add(x.Name));
    }
}
CuriousPen
  • 249
  • 1
  • 8
  • That would never work, unless he will be millisecond precise. Also too much processor overhead, why enumerating the sequence twice? – Matan Shahar May 11 '14 at 09:07