http://www.csharpwin.com/dotnetspace/12018r482.shtml
前台文件file.aspx
<% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " file.aspx.cs " Inherits = " file " %> <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > < html xmlns ="http://www.w3.org/1999/xhtml" > < head runat ="server" > < title >无标题页 </ title > </ head > < body > < form id ="form1" runat ="server" > < div > < table width ="100%" border ="0" cellspacing ="0" > < tr > < td height ="60" colspan ="2" align ="center" bgcolor ="#CCCCCC" >文件管理器 </ td > </ tr > < tr > < td height ="25" >当前路径: < asp:Label ID ="Label2" runat ="server" Text ="Label" ></ asp:Label ></ td > < td width ="200" > < span style ="color: crimson" >< strong >返回上一层 </ strong ></ span >< asp:ImageButton ID ="ImageButton1" runat ="server" ImageUrl ="~/ZA012330292052.gif" OnClick ="ImageButton1_Click" /></ td > </ tr > < tr > < td colspan ="2" style ="height: 65px" > < asp:Table ID ="Table1" runat ="server" > </ asp:Table > </ td > </ tr > </ table > </ div > </ form > </ body > </ html >
CS文件
public partial class file : System.Web.UI.Page { public string fpath; protected void Page_Load( object sender, EventArgs e) { if (!IsPostBack) { if (Request.QueryString[ " fpath "] == null) { fpath = Server.MapPath( " . "); this.Label2.Text = fpath; BindFile(fpath); } else { fpath = Server.UrlDecode(Request.QueryString[ " fpath "].ToString()); this.Label2.Text=fpath; BindFile(fpath); } } } /// <summary> /// /// </summary> /// <param name="fpath"></param> protected void BindFile( string path) { TableCell td; TableRow tr; tr = new TableRow(); td = new TableCell(); td.Text = " 文件名 "; td.Width= 300; td.BackColor = Color.Aqua; tr.Cells.Add(td); td = new TableCell(); td.Text = " 大小 "; td.Width = 100; td.BackColor = Color.Aqua; tr.Cells.Add(td); td = new TableCell(); td.Text = " 类型 "; td.Width = 100; td.BackColor = Color.Aqua; tr.Cells.Add(td); td = new TableCell(); td.Text = " 发布时间 "; td.Width = 200; td.BackColor = Color.Aqua; tr.Cells.Add(td); td = new TableCell(); td.Text = " 操作 "; td.Width = 100; td.BackColor = Color.Aqua; tr.Cells.Add(td); Table1.Rows.Add(tr); string fname; string ftype; string ftime; string fsize; HyperLink hlink; string url; DirectoryInfo di = new DirectoryInfo(path); foreach(FileSystemInfo fsi in di.GetFileSystemInfos()) { if (fsi is FileInfo) // 如果是文件 { FileInfo fi = (FileInfo)fsi; fname = fi.Name; if (fi.Extension.Length < 1) // 如果扩展名小于一 { ftype = ""; } else { ftype = fi.Extension.Remove( 0, 1); } fsize = fi.Length.ToString(); ftime = fi.LastWriteTime.ToString(); } else // 如果是文件夹 { di = (DirectoryInfo)fsi; fname = di.Name; ftype = " 文件夹 "; fsize = ""; ftime = di.LastWriteTime.ToString(); } tr = new TableRow(); td = new TableCell(); td.Controls.Add( new LiteralControl(fname)); tr.Cells.Add(td); td = new TableCell(); td.Controls.Add( new LiteralControl(ftype)); tr.Cells.Add(td); td = new TableCell(); td.Controls.Add( new LiteralControl(fsize)); tr.Cells.Add(td); td = new TableCell(); td.Controls.Add( new LiteralControl(ftime)); tr.Cells.Add(td); if (fsi is DirectoryInfo) { hlink = new HyperLink(); hlink.Text = " 打开 "; url = Server.UrlEncode(fpath); hlink.NavigateUrl = " file.aspx?fpath= " + url + " \\ " + fname; td = new TableCell(); td.Controls.Add(hlink); tr.Cells.Add(td); } else { hlink = new HyperLink(); hlink.Text = " 编辑 "; url = Server.UrlEncode(fpath); hlink.NavigateUrl = " edit.aspx?fpath= " + url + " \\ " + fname; td = new TableCell(); td.Controls.Add(hlink); tr.Cells.Add(td); } Table1.Rows.Add(tr); } } protected void ImageButton1_Click( object sender, ImageClickEventArgs e) { if (fpath != Label2.Text.ToString()) { string rootdir = Directory.GetParent(Label2.Text).FullName; fpath = rootdir; this.Label2.Text = fpath; BindFile(fpath); } } }
注意需要引入对应的命名空间: 关于对文件处理和编辑这里不就叙述了.基本原理是利用IO打开传过来文件的路径的文件 然后编辑后保存就可以了
一个简单的文件浏览器就做好了 可以浏览服务器上的文件
最后效果:
作者:匿名