遍历文件夹所有文件!



<asp:GridView ID="GridView1" runat="server" Width="632px" AutoGenerateColumns="False">
            
<Columns>
                
<asp:TemplateField HeaderText="文件名">
                    
<ItemTemplate>
                        
<a href='<%#Eval("FileShowPath") %>' target="_blank"><%#Eval("FileName"%></a>
                    
</ItemTemplate>
                
</asp:TemplateField>
                
<asp:TemplateField HeaderText="文件类型">
                     
<ItemTemplate>
                        
<%#Eval("FileType"%>
                    
</ItemTemplate>
                
</asp:TemplateField>
                
<asp:TemplateField HeaderText="文件大小">
                     
<ItemTemplate>
                        
<%#Eval("FileSize"%>
                    
</ItemTemplate>
                
</asp:TemplateField>
                
<asp:TemplateField HeaderText="文件修改时间">
                     
<ItemTemplate>
                        
<%#Eval("FileLastWrite"%>
                     
</ItemTemplate>
                
</asp:TemplateField>
            
</Columns>
        
</asp:GridView>


public class AppCom
{
    
public AppCom()
    
{
        
//
        
// TODO: 在此处添加构造函数逻辑
        
//
    }

    
private string fileName;

    
public string FileName
    
{
        
get return fileName; }
        
set { fileName = value; }
    }

    
private string fileType;

    
public string FileType
    
{
        
get return fileType; }
        
set { fileType=value; }
    }

    
private string fileSize;

    
public string FileSize
    
{
        
get return fileSize; }
        
set { fileSize = value; }
    }

    
private string fileLastWrite;
    
public string FileLastWrite
    
{
        
get return fileLastWrite; }
        
set { fileLastWrite = value; }
    }

    
private string fileShowPath;
    
public string FileShowPath
    
{
        
get return fileShowPath; }
        
set { fileShowPath = value; }
    }

}

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public partial class UploadFileManage : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            
this.GridView1.DataSource = bindGridView();
            
this.GridView1.DataBind();
        }

    }

    
private List<AppCom> bindGridView()
    
{
        
string strPath = Server.MapPath(@"~/Images/");
        
string showPath = AppDomain.CurrentDomain.BaseDirectory.Substring(0,AppDomain.CurrentDomain.BaseDirectory.Length-1);
        showPath
=showPath.Substring(showPath.LastIndexOf("\\")+1);
        showPath 
= @"http://" + Request.Url.Authority + "/"+showPath + "/Images/";
        
//string showPath = Request.Url.AbsoluteUri.Substring(0, 31) + "/Images/"+Request.Url.Authority;
        
//string showPath = Page.Request.PhysicalApplicationPath + "Images\\";
        List<AppCom> fileList = new List<AppCom>();
        DirectoryInfo di 
= new DirectoryInfo(strPath);
        FileInfo[] files;
        
try
        
{
            files 
= di.GetFiles();
            
foreach (FileInfo fi in files)
            
{
                AppCom ac 
= new AppCom();
                ac.FileName 
= fi.Name;
                ac.FileSize 
= Convert.ToString(fi.Length);
                ac.FileType 
= fi.Extension;
                ac.FileLastWrite 
= fi.LastWriteTime.ToString();
                ac.FileShowPath 
= showPath + fi.Name;
                fileList.Add(ac);
            }

            
return fileList;
        }

        
catch (Exception)
        
{

            
throw;
        }

    }

    
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    
{
        
//string fileName = this.GridView1.DataKeys[e.RowIndex].Values[0].ToString();
       
// string fileName = this.GridView1.Rows[e.RowIndex].Cells[0].Text+this.GridView1.Rows[e.RowIndex].Cells[1].Text;
        string fileName = ((DataBoundLiteralControl)(this.GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.Trim();
        Match m 
= Regex.Match(fileName, @"(?<=<a[^>]*>)[\s\S]+?(?=</a>)", RegexOptions.IgnoreCase);
        
if (m.Success)
        
{
            fileName 
= m.Value;
        }

        File.Delete(Server.MapPath(
@"~/Images/"+ fileName);
        
this.GridView1.DataSource = bindGridView();
        
this.GridView1.DataBind();
        
//List<AppCom> la = new List<AppCom>();
        
    }

}


posted @ 2008-03-24 16:34  阳光囧男  阅读(808)  评论(0编辑  收藏  举报