YAGYU JUBEI

逝去的流云散尽 拥有的一生抓紧
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

[ASP.NET]在服务器相对路径下简单搜索文件

Posted on 2005-11-22 13:51  YAGYU JUBEI  阅读(1571)  评论(0)    收藏  举报
用于asp.net应用程序中基于相对路径的一个文件夹下的文件搜索.
按文件名包括后缀分类搜索(不包括子文件夹).
一个textbox控件 txtSearth 用于填入搜索文件名或者部分文件名.
输出格式代码较多 , 程序主要是获取当前程序路径 , 我获取的就是程序所在目录,并搜索该目录下文件. 1-5行
设置文件搜索类别 10 行
"*" + this.txtSearth.Text + "*" + ".dfd"   表示我当前搜索的是文件名或包含文件名 ** 并且后缀为 .dfd 的文件 . 大家使用时可自己修改 .

 1String 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;    

输出如下:

File name Extension Path 大小(Kb) 时间

葡南扶余综合图7.9.dfd

.dfd /result 2993 2005-8-18 13:21:20

头台.dfd

.dfd /result 201 2005-8-18 13:21:34

永乐扶.dfd

.dfd /result 5706 2005-8-18 13:11:16

T2构造图.dfd

.dfd /result 3041 2005-8-18 13:20:24

厂界.dfd

.dfd /result 367 2004-8-15 13:06:54