如何使用C#修改日期将搜索文件列入列表框? [英] How to list the search files into listbox by date modified using C# ?

查看:63
本文介绍了如何使用C#修改日期将搜索文件列入列表框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



这里我有一个问题,就是根据修改日期将搜索文件列入列表框。以下代码仅显示将搜索文件列入列表框。谁能帮我解决这个问题呢.....



Hi Friends,

Here i have a problem with to list the search files into the listbox according to the date modified. The below code is shows only list the search files into the listbox. Could anyone help me how settle this problem please.....

protected void Button3_Click(object sender, EventArgs e)
        {
                ListBox2.Items.Clear();

                string search = TextBox1.Text;
                if (search != "")
                {
var result = Directory.GetFiles(@"\\192.126.8.118\test\exam\Result", "*" + search + "*.txt").Union(Directory.GetFiles(@"\\192.126.8.118\test\exam\Result", "*" + search + "*.csv"));
                    foreach (string file in result)
                    {
                        ListBox2.Items.Add(new ListItem(Path.GetFileName(file), file));
                    }
                    {
                        search = "";
                    }
                }
                else
                {
                    Response.Write("<script>alert('Please Enter Search Keyword');</script>");
                }
                //if(ListBox2.Items.Count!=0)
                //{
                //    Response.Write("<script>alert('Search Found');</script>");
                //}
                if (ListBox2.Items.Count == 0)
                {
                    Response.Write("<script>alert('Sorry!! SearchNot Found');</script>");
                }


        }

推荐答案

string path = @"D:\bk";
string searchPattern = "*.txt";

DirectoryInfo di = new DirectoryInfo(path);
DirectoryInfo[] directories =
    di.GetDirectories(searchPattern, SearchOption.AllDirectories);

var result =
    di.GetFiles(searchPattern, SearchOption.AllDirectories).OrderBy(f => f.LastWriteTime);
foreach (FileInfo file in result)
{
     ListBox2.Items.Add(new ListItem(Path.GetFileName(file.FullName), file.FullName));
}


这篇关于如何使用C#修改日期将搜索文件列入列表框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆