string s = "", s1 = ""; //to get the file name // string files = ""; string filename = "*" + tb_search.Text + "*"; DirectoryInfo MyDir; ArrayList CellarList = new ArrayList(); MyDir = new DirectoryInfo(Server.MapPath("files/")); string path = (Server.MapPath("files/")); //FileInfo[] MyFiles = MyDir.GetFiles("*.*"); string[] fileList = System.IO.Directory.GetFiles(path, filename); foreach (string file in fileList) { if (file.ToLower().Contains(tb_search.Text.ToLower().ToString())) { s = file.ToString(); s1 = s.Replace(path, ""); lb_search.Items.Add(s1); } }
.aspx file <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox> .cs file protected void Button1_Click(object sender, EventArgs e) { ListBox1.Items.Clear(); DirSearch("c:\\test"); } void DirSearch(string sDir) { foreach (string d in Directory.GetDirectories(sDir)) { foreach (string f in Directory.GetFiles(d, TextBox1.Text)) { ListBox1.Items.Add(f);//if find the file, then put it into the listbox } DirSearch(d); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)