用于asp.net应用程序中基于相对路径的一个文件夹下的文件搜索.
按文件名包括后缀分类搜索(不包括子文件夹).
一个textbox控件 txtSearth 用于填入搜索文件名或者部分文件名.
输出格式代码较多 , 程序主要是获取当前程序路径 , 我获取的就是程序所在目录,并搜索该目录下文件. 1-5行
设置文件搜索类别 10 行
"*" + this.txtSearth.Text + "*" + ".dfd" 表示我当前搜索的是文件名或包含文件名 ** 并且后缀为 .dfd 的文件 . 大家使用时可自己修改 .
输出如下:
按文件名包括后缀分类搜索(不包括子文件夹).
一个textbox控件 txtSearth 用于填入搜索文件名或者部分文件名.
输出格式代码较多 , 程序主要是获取当前程序路径 , 我获取的就是程序所在目录,并搜索该目录下文件. 1-5行
设置文件搜索类别 10 行
"*" + this.txtSearth.Text + "*" + ".dfd" 表示我当前搜索的是文件名或包含文件名 ** 并且后缀为 .dfd 的文件 . 大家使用时可自己修改 .
1
String path = Request.PhysicalApplicationPath;//获取当前服务程序下根目录物理路径
2
String fullpath = this.TemplateSourceDirectory;//获取当前虚拟目录
3
Int32 counter = 0;//行数
4
5
DirectoryInfo dir = new DirectoryInfo(path);
6
Response.Output.Write("<br><br><br><br><br>");
7
//创建表头
8
Response.Output.WriteLine("<table " + "align=center" + " cellpadding = '2' " + "cellspacing = '4'><tr><th>File name</th><th>"+ "Extension </th><th>Path</th> <th>大小(Kb)</th><th> 时间 </th></tr>");
9
10
foreach (FileInfo f in dir.GetFiles("*" + this.txtSearth.Text + "*" + ".dfd"))//确定搜索文件类别
11
{
12
Response.Write("<tr");
13
if(counter % 2==0)//设置换行格式
14
{
15
Response.Write(" style = 'background: " + "lightyellow'>");
16
}
17
else
18
{
19
Response.Write(" style = 'background: lightblue'>");
20
}
21
22
//填充数据
23
Response.Write("<td>");
24
Response.Write("<p><a href=");
25
Response.Write(fullpath + "/");
26
Response.Write(f.Name);
27
Response.Write(">");
28
Response.Write(f.Name + "</td>");
29
Response.Output.WriteLine("</a>");
30
Response.Write("<td>" + f.Extension + "</td>");
31
Response.Write("<td>" + fullpath + "</td>");
32
Response.Write("<td>" + f.Length/1024 + "</td>");
33
Response.Write("<td>" + f.LastWriteTime + "</td>");
34
Response.Output.WriteLine("</p>");
35
counter += 1;
String path = Request.PhysicalApplicationPath;//获取当前服务程序下根目录物理路径2
String fullpath = this.TemplateSourceDirectory;//获取当前虚拟目录3
Int32 counter = 0;//行数4

5
DirectoryInfo dir = new DirectoryInfo(path);6
Response.Output.Write("<br><br><br><br><br>");7
//创建表头8
Response.Output.WriteLine("<table " + "align=center" + " cellpadding = '2' " + "cellspacing = '4'><tr><th>File name</th><th>"+ "Extension </th><th>Path</th> <th>大小(Kb)</th><th> 时间 </th></tr>");9
10
foreach (FileInfo f in dir.GetFiles("*" + this.txtSearth.Text + "*" + ".dfd"))//确定搜索文件类别11
{12
Response.Write("<tr");13
if(counter % 2==0)//设置换行格式14
{15
Response.Write(" style = 'background: " + "lightyellow'>");16
} 17
else18
{19
Response.Write(" style = 'background: lightblue'>");20
}21
22
//填充数据23
Response.Write("<td>");24
Response.Write("<p><a href=");25
Response.Write(fullpath + "/");26
Response.Write(f.Name);27
Response.Write(">");28
Response.Write(f.Name + "</td>");29
Response.Output.WriteLine("</a>");30
Response.Write("<td>" + f.Extension + "</td>");31
Response.Write("<td>" + fullpath + "</td>");32
Response.Write("<td>" + f.Length/1024 + "</td>");33
Response.Write("<td>" + f.LastWriteTime + "</td>");34
Response.Output.WriteLine("</p>");35
counter += 1; 输出如下:
| File name | Extension | Path | 大小(Kb) | 时间 |
|---|---|---|---|---|
| .dfd | /result | 2993 | 2005-8-18 13:21:20 | |
| .dfd | /result | 201 | 2005-8-18 13:21:34 | |
| .dfd | /result | 5706 | 2005-8-18 13:11:16 | |
| .dfd | /result | 3041 | 2005-8-18 13:20:24 | |
| .dfd | /result | 367 | 2004-8-15 13:06:54 |



浙公网安备 33010602011771号