会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
′懿™
关注就是力量,围观改变中国
博客园
首页
新随笔
联系
管理
订阅
遍历文件夹所有文件!
<
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
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告