一个非常经典的asp.net2.0文件管理源代码,不知道以前各位是否看到.但我还是放上来,所谓经典就是说它易懂,容易让人看透.呵呵.对于新手很有帮助.在这里帖出代码.供大家学习,有些不足的话大家自己补充一下哦.
filemanage.aspx
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="filemanage.aspx.cs" Inherits="filemanage" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>Untitled Page</title>
8 <script language="javascript">
9 function delfile(path)
10 {
11 if(window.confirm("是否确定删除此文件?/n"+path))
12 {
13 location.href="del.aspx?type=1&path="+path;
14 }
15 }
16 function delfolder(path)
17 {
18 if(window.confirm("是否确定删除此文件夹?/n"+path))
19 {
20 location.href="del.aspx?type=2&path="+path;
21 }
22 }
23 function movefile(path)
24 {
25 aa=window.prompt("请输入您要修改的文件名",path);
26 location.href="move.aspx?type=1&path="+path+"&target="+aa;
27 }
28 function movefolder(path)
29 {
30 aa=window.prompt("请输入您要修改的文件夹名",path);
31 location.href="move.aspx?type=2&path="+path+"&target="+aa;
32 }
33 </script>
34
35</head>
36<body>
37 <form id="form1" runat="server">
38 <div>
39 <asp:Label id="Label5" runat="server" Height="11px"></asp:Label><br />
40 <asp:Label id="Label6" runat="server"></asp:Label><br />
41 <asp:Label id="Label7" runat="server"></asp:Label><br />
42 <asp:label id="Label1" runat="server" CssClass="text"></asp:label><br />
43 <asp:label id="Label3" runat="server" CssClass="text"></asp:label><br />
44 <asp:label id="Label4" runat="server" CssClass="text"></asp:label><asp:table id="Table1" runat="server" Width="100%">
45 <asp:TableRow ForeColor="White" BackColor="#5F8AC5">
46 <asp:TableCell HorizontalAlign="Center" Text="文件名"></asp:TableCell>
47 <asp:TableCell HorizontalAlign="Center" Text="大小"></asp:TableCell>
48 <asp:TableCell HorizontalAlign="Center" Text="属性"></asp:TableCell>
49 <asp:TableCell HorizontalAlign="Center" Text="建立时间"></asp:TableCell>
50 <asp:TableCell HorizontalAlign="Center" Text="最后访问时间"></asp:TableCell>
51 <asp:TableCell HorizontalAlign="Center" Text="最后修改时间"></asp:TableCell>
52 <asp:TableCell HorizontalAlign="Center" Text="删除"></asp:TableCell>
53 <asp:TableCell HorizontalAlign="Center" Text="称动/重命名"></asp:TableCell>
54 </asp:TableRow>
55 </asp:table>
56 <asp:Label id="Label2" runat="server" CssClass="text"></asp:Label></FONT></form>
57 </div>
58 </form>
59</body>
60</html>
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="filemanage.aspx.cs" Inherits="filemanage" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>Untitled Page</title>
8 <script language="javascript">
9 function delfile(path)
10 {
11 if(window.confirm("是否确定删除此文件?/n"+path))
12 {
13 location.href="del.aspx?type=1&path="+path;
14 }
15 }
16 function delfolder(path)
17 {
18 if(window.confirm("是否确定删除此文件夹?/n"+path))
19 {
20 location.href="del.aspx?type=2&path="+path;
21 }
22 }
23 function movefile(path)
24 {
25 aa=window.prompt("请输入您要修改的文件名",path);
26 location.href="move.aspx?type=1&path="+path+"&target="+aa;
27 }
28 function movefolder(path)
29 {
30 aa=window.prompt("请输入您要修改的文件夹名",path);
31 location.href="move.aspx?type=2&path="+path+"&target="+aa;
32 }
33 </script>
34
35</head>
36<body>
37 <form id="form1" runat="server">
38 <div>
39 <asp:Label id="Label5" runat="server" Height="11px"></asp:Label><br />
40 <asp:Label id="Label6" runat="server"></asp:Label><br />
41 <asp:Label id="Label7" runat="server"></asp:Label><br />
42 <asp:label id="Label1" runat="server" CssClass="text"></asp:label><br />
43 <asp:label id="Label3" runat="server" CssClass="text"></asp:label><br />
44 <asp:label id="Label4" runat="server" CssClass="text"></asp:label><asp:table id="Table1" runat="server" Width="100%">
45 <asp:TableRow ForeColor="White" BackColor="#5F8AC5">
46 <asp:TableCell HorizontalAlign="Center" Text="文件名"></asp:TableCell>
47 <asp:TableCell HorizontalAlign="Center" Text="大小"></asp:TableCell>
48 <asp:TableCell HorizontalAlign="Center" Text="属性"></asp:TableCell>
49 <asp:TableCell HorizontalAlign="Center" Text="建立时间"></asp:TableCell>
50 <asp:TableCell HorizontalAlign="Center" Text="最后访问时间"></asp:TableCell>
51 <asp:TableCell HorizontalAlign="Center" Text="最后修改时间"></asp:TableCell>
52 <asp:TableCell HorizontalAlign="Center" Text="删除"></asp:TableCell>
53 <asp:TableCell HorizontalAlign="Center" Text="称动/重命名"></asp:TableCell>
54 </asp:TableRow>
55 </asp:table>
56 <asp:Label id="Label2" runat="server" CssClass="text"></asp:Label></FONT></form>
57 </div>
58 </form>
59</body>
60</html>
下面是CS文件哦。
filemanage.aspx.cs
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.IO;
12
13
14public partial class filemanage : System.Web.UI.Page
15{
16 protected void Page_Load(object sender, EventArgs e)
17 {
18 // 在此处放置用户代码以初始化页面
19 if (!this.IsPostBack)
20 {
21 ListFile();
22 }
23 }
24
25 private void ListFile()
26 {
27 //接收路径
28 string strPath = Request.QueryString["path"];
29 //系统标识符和版本号
30 string strSystem = Environment.OSVersion.ToString();
31 //获取映射到进程上下文的物理内存量
32 string strRem = Environment.WorkingSet.ToString();
33 //获取系统启动后经过的毫秒数
34 int iTC = Environment.TickCount / 60000;
35 //系统目录的完全限定路径
36 string strSD = Environment.SystemDirectory;
37 //获取此本地计算机的 NetBIOS 名称
38 string strMN = Environment.MachineName;
39 //获取与当前用户关联的网络域名
40 string strUDN = Environment.UserDomainName;
41
42 this.Label5.Text = "系统:" + strSystem;
43 this.Label6.Text = "可用内存:" + strRem;
44 this.Label7.Text = "自上次重启已有:" + iTC + "分钟了";
45 this.Label7.Text = this.Label7.Text + "<br>系统路径:<a href='filemanage.aspx?path=" + strSD + "'>" + strSD + "</a>";
46 this.Label7.Text = this.Label7.Text + "<br>计算机的名称:" + strMN;
47 this.Label7.Text = this.Label7.Text + "<br>网络域名:" + strUDN;
48
49 //如果接收的路径为空,则获取当前路径
50 if (strPath == null)
51 {
52 strPath = Request.ServerVariables["APPL_PHYSICAL_PATH"] + "//" + "FileFloder";
53 }
54 else
55 {
56 if (strPath.Substring(strPath.Length - 1, 1) != "//")
57 {
58 strPath = strPath + "//";
59 }
60 }
61 this.Label1.Text = "当目录:" + strPath;
62
63 try
64 {
65 //输出所有驱动器号
66 string[] drv = System.IO.Directory.GetLogicalDrives();
67 this.Label3.Text = "驱动器:";
68 for (int i = 0; i < drv.Length - 1; i++)
69 {
70 this.Label3.Text = this.Label3.Text + "<a href='filemanage.aspx?path=" + drv[i] + "'>" + drv[i] + "</a> ";
71 }
72
73 //当前目录下的所有文件夹
74 DirectoryInfo di = new DirectoryInfo(strPath);
75 DirectoryInfo[] wjj = di.GetDirectories();
76
77 //上级目录
78 if (strPath.Substring(strPath.Length - 2, 2) != "://")
79 {
80 string[] aa = strPath.Split('//');
81 string bb = "";
82 for (int i = 0; i < aa.Length - 2; i++)
83 {
84 if (i == 0)
85 {
86 bb = aa[i];
87 }
88 else
89 {
90 bb = bb + "//" + aa[i];
91 }
92 }
93 this.Label4.Text = "<br><a href='filemanage.aspx?path=" + bb + "'>上级目录:..</a> ";
94 }
95
96
97 //输出当前目录下的所有文件夹
98 foreach (DirectoryInfo diTemp in wjj)
99 {
100 TableRow tr = new TableRow();
101 //tr.BackColor=ColorTranslator.FromHtml("#dddddd");
102 tr.Attributes.Add("class", "folder");
103 tr.Attributes.Add("onmouseover", "this.className='2'");
104 tr.Attributes.Add("onmouseout", "this.className='folder'");
105
106 TableCell td = new TableCell();
107
108 //文件夹名
109 td = new TableCell();
110 td.Text = "<a href='filemanage.aspx?path=" + strPath + diTemp.Name + "'>" + diTemp.Name + "</a>";
111 tr.Cells.Add(td);
112
113 //空列
114 td = new TableCell();
115 tr.Cells.Add(td);
116
117 //属性
118 td = new TableCell();
119 td.Text = diTemp.Attributes.ToString().Replace("Archive", "存档").Replace("Compressed", "压缩").Replace("Device", "保留").Replace("Directory", "目录").Replace("Encrypted", "加密").Replace("Hidden", "隐藏").Replace("Normal", "正常").Replace("NotContentIndexed", "非索引").Replace("Offline", "脱机").Replace("ReadOnly", "只读").Replace("ReparsePoint", "重新分析点").Replace("SparseFile", "稀疏").Replace("System", "系统").Replace("Temporary", "临时");
120 tr.Cells.Add(td);
121 //建立时间
122 td = new TableCell();
123 td.Text = diTemp.CreationTime.ToString();
124 tr.Cells.Add(td);
125 //最后访问时间
126 td = new TableCell();
127 td.Text = diTemp.LastAccessTime.ToString();
128 tr.Cells.Add(td);
129 //最后修改时间
130 td = new TableCell();
131 td.Text = diTemp.LastWriteTime.ToString();
132 tr.Cells.Add(td);
133 //删除
134 string strBack = strPath + diTemp.Name;
135 strBack = strBack.Replace("//", "");
136 td = new TableCell();
137 td.Text = "<a href=# οnclick=/"javascript:delfolder('" + strBack + "')/">删除</a>";
138 td.HorizontalAlign = HorizontalAlign.Center;
139 tr.Cells.Add(td);
140 //移动或重命名
141 td = new TableCell();
142 td.Text = "<a href=# οnclick=/"javascript:movefolder('" + strBack + "')/">移动/重命名</a>";
143 td.HorizontalAlign = HorizontalAlign.Center;
144 tr.Cells.Add(td);
145 this.Table1.Rows.Add(tr);
146
147 }
148
149 //当前目录下的所有文件
150 FileInfo[] fi = di.GetFiles();
151 foreach (FileInfo fiTemp in fi)
152 {
153
154 TableRow tr = new TableRow();
155 //tr.BackColor=ColorTranslator.FromHtml("#eeeeee");
156 tr.Attributes.Add("class", "file");
157 tr.Attributes.Add("onmouseover", "this.className='2'");
158 tr.Attributes.Add("onmouseout", "this.className='file'");
159 TableCell td = new TableCell();
160
161 //文件名
162 td = new TableCell();
163 td.Text = "<a href='read.aspx?path=" + strPath + fiTemp.Name + "'target=_blank><font color=#000000>" + fiTemp.Name + "</font></a>";
164 tr.Cells.Add(td);
165 //大小
166 td = new TableCell();
167 td.Text = fiTemp.Length.ToString();
168 td.HorizontalAlign = HorizontalAlign.Right;
169 tr.Cells.Add(td);
170 //属性
171 td = new TableCell();
172 td.Text = fiTemp.Attributes.ToString().Replace("Archive", "存档").Replace("Compressed", "压缩").Replace("Device", "保留").Replace("Directory", "目录").Replace("Encrypted", "加密").Replace("Hidden", "隐藏").Replace("Normal", "正常").Replace("NotContentIndexed", "非索引").Replace("Offline", "脱机").Replace("ReadOnly", "只读").Replace("ReparsePoint", "重新分析点").Replace("SparseFile", "稀疏").Replace("System", "系统").Replace("Temporary", "临时");
173 tr.Cells.Add(td);
174 //建立时间
175 td = new TableCell();
176 td.Text = fiTemp.CreationTime.ToString();
177 tr.Cells.Add(td);
178 //最后访问时间
179 td = new TableCell();
180 td.Text = fiTemp.LastAccessTime.ToString();
181 tr.Cells.Add(td);
182 //最后修改时间
183 td = new TableCell();
184 td.Text = fiTemp.LastWriteTime.ToString();
185 tr.Cells.Add(td);
186 //删除
187 string strBack = strPath + fiTemp.Name;
188 strBack = strBack.Replace("//", "");
189 td = new TableCell();
190 td.Text = "<a href=# οnclick=/"javascript:delfile('" + strBack + "')/">删除</a>";
191 td.HorizontalAlign = HorizontalAlign.Center;
192 tr.Cells.Add(td);
193 //移动或重命名
194 td = new TableCell();
195 td.Text = "<a href=# οnclick=/"javascript:movefile('" + strBack + "')/">移动/重命名</a>";
196 td.HorizontalAlign = HorizontalAlign.Center;
197 tr.Cells.Add(td);
198 this.Table1.Rows.Add(tr);
199
200 }
201 }
202 catch (Exception ex)
203 {
204 this.Label2.Text = "<br><p align=center>" + ex.Message + "</p>";
205 }
206 }
207
208}
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.IO;
12
13
14public partial class filemanage : System.Web.UI.Page
15{
16 protected void Page_Load(object sender, EventArgs e)
17 {
18 // 在此处放置用户代码以初始化页面
19 if (!this.IsPostBack)
20 {
21 ListFile();
22 }
23 }
24
25 private void ListFile()
26 {
27 //接收路径
28 string strPath = Request.QueryString["path"];
29 //系统标识符和版本号
30 string strSystem = Environment.OSVersion.ToString();
31 //获取映射到进程上下文的物理内存量
32 string strRem = Environment.WorkingSet.ToString();
33 //获取系统启动后经过的毫秒数
34 int iTC = Environment.TickCount / 60000;
35 //系统目录的完全限定路径
36 string strSD = Environment.SystemDirectory;
37 //获取此本地计算机的 NetBIOS 名称
38 string strMN = Environment.MachineName;
39 //获取与当前用户关联的网络域名
40 string strUDN = Environment.UserDomainName;
41
42 this.Label5.Text = "系统:" + strSystem;
43 this.Label6.Text = "可用内存:" + strRem;
44 this.Label7.Text = "自上次重启已有:" + iTC + "分钟了";
45 this.Label7.Text = this.Label7.Text + "<br>系统路径:<a href='filemanage.aspx?path=" + strSD + "'>" + strSD + "</a>";
46 this.Label7.Text = this.Label7.Text + "<br>计算机的名称:" + strMN;
47 this.Label7.Text = this.Label7.Text + "<br>网络域名:" + strUDN;
48
49 //如果接收的路径为空,则获取当前路径
50 if (strPath == null)
51 {
52 strPath = Request.ServerVariables["APPL_PHYSICAL_PATH"] + "//" + "FileFloder";
53 }
54 else
55 {
56 if (strPath.Substring(strPath.Length - 1, 1) != "//")
57 {
58 strPath = strPath + "//";
59 }
60 }
61 this.Label1.Text = "当目录:" + strPath;
62
63 try
64 {
65 //输出所有驱动器号
66 string[] drv = System.IO.Directory.GetLogicalDrives();
67 this.Label3.Text = "驱动器:";
68 for (int i = 0; i < drv.Length - 1; i++)
69 {
70 this.Label3.Text = this.Label3.Text + "<a href='filemanage.aspx?path=" + drv[i] + "'>" + drv[i] + "</a> ";
71 }
72
73 //当前目录下的所有文件夹
74 DirectoryInfo di = new DirectoryInfo(strPath);
75 DirectoryInfo[] wjj = di.GetDirectories();
76
77 //上级目录
78 if (strPath.Substring(strPath.Length - 2, 2) != "://")
79 {
80 string[] aa = strPath.Split('//');
81 string bb = "";
82 for (int i = 0; i < aa.Length - 2; i++)
83 {
84 if (i == 0)
85 {
86 bb = aa[i];
87 }
88 else
89 {
90 bb = bb + "//" + aa[i];
91 }
92 }
93 this.Label4.Text = "<br><a href='filemanage.aspx?path=" + bb + "'>上级目录:..</a> ";
94 }
95
96
97 //输出当前目录下的所有文件夹
98 foreach (DirectoryInfo diTemp in wjj)
99 {
100 TableRow tr = new TableRow();
101 //tr.BackColor=ColorTranslator.FromHtml("#dddddd");
102 tr.Attributes.Add("class", "folder");
103 tr.Attributes.Add("onmouseover", "this.className='2'");
104 tr.Attributes.Add("onmouseout", "this.className='folder'");
105
106 TableCell td = new TableCell();
107
108 //文件夹名
109 td = new TableCell();
110 td.Text = "<a href='filemanage.aspx?path=" + strPath + diTemp.Name + "'>" + diTemp.Name + "</a>";
111 tr.Cells.Add(td);
112
113 //空列
114 td = new TableCell();
115 tr.Cells.Add(td);
116
117 //属性
118 td = new TableCell();
119 td.Text = diTemp.Attributes.ToString().Replace("Archive", "存档").Replace("Compressed", "压缩").Replace("Device", "保留").Replace("Directory", "目录").Replace("Encrypted", "加密").Replace("Hidden", "隐藏").Replace("Normal", "正常").Replace("NotContentIndexed", "非索引").Replace("Offline", "脱机").Replace("ReadOnly", "只读").Replace("ReparsePoint", "重新分析点").Replace("SparseFile", "稀疏").Replace("System", "系统").Replace("Temporary", "临时");
120 tr.Cells.Add(td);
121 //建立时间
122 td = new TableCell();
123 td.Text = diTemp.CreationTime.ToString();
124 tr.Cells.Add(td);
125 //最后访问时间
126 td = new TableCell();
127 td.Text = diTemp.LastAccessTime.ToString();
128 tr.Cells.Add(td);
129 //最后修改时间
130 td = new TableCell();
131 td.Text = diTemp.LastWriteTime.ToString();
132 tr.Cells.Add(td);
133 //删除
134 string strBack = strPath + diTemp.Name;
135 strBack = strBack.Replace("//", "");
136 td = new TableCell();
137 td.Text = "<a href=# οnclick=/"javascript:delfolder('" + strBack + "')/">删除</a>";
138 td.HorizontalAlign = HorizontalAlign.Center;
139 tr.Cells.Add(td);
140 //移动或重命名
141 td = new TableCell();
142 td.Text = "<a href=# οnclick=/"javascript:movefolder('" + strBack + "')/">移动/重命名</a>";
143 td.HorizontalAlign = HorizontalAlign.Center;
144 tr.Cells.Add(td);
145 this.Table1.Rows.Add(tr);
146
147 }
148
149 //当前目录下的所有文件
150 FileInfo[] fi = di.GetFiles();
151 foreach (FileInfo fiTemp in fi)
152 {
153
154 TableRow tr = new TableRow();
155 //tr.BackColor=ColorTranslator.FromHtml("#eeeeee");
156 tr.Attributes.Add("class", "file");
157 tr.Attributes.Add("onmouseover", "this.className='2'");
158 tr.Attributes.Add("onmouseout", "this.className='file'");
159 TableCell td = new TableCell();
160
161 //文件名
162 td = new TableCell();
163 td.Text = "<a href='read.aspx?path=" + strPath + fiTemp.Name + "'target=_blank><font color=#000000>" + fiTemp.Name + "</font></a>";
164 tr.Cells.Add(td);
165 //大小
166 td = new TableCell();
167 td.Text = fiTemp.Length.ToString();
168 td.HorizontalAlign = HorizontalAlign.Right;
169 tr.Cells.Add(td);
170 //属性
171 td = new TableCell();
172 td.Text = fiTemp.Attributes.ToString().Replace("Archive", "存档").Replace("Compressed", "压缩").Replace("Device", "保留").Replace("Directory", "目录").Replace("Encrypted", "加密").Replace("Hidden", "隐藏").Replace("Normal", "正常").Replace("NotContentIndexed", "非索引").Replace("Offline", "脱机").Replace("ReadOnly", "只读").Replace("ReparsePoint", "重新分析点").Replace("SparseFile", "稀疏").Replace("System", "系统").Replace("Temporary", "临时");
173 tr.Cells.Add(td);
174 //建立时间
175 td = new TableCell();
176 td.Text = fiTemp.CreationTime.ToString();
177 tr.Cells.Add(td);
178 //最后访问时间
179 td = new TableCell();
180 td.Text = fiTemp.LastAccessTime.ToString();
181 tr.Cells.Add(td);
182 //最后修改时间
183 td = new TableCell();
184 td.Text = fiTemp.LastWriteTime.ToString();
185 tr.Cells.Add(td);
186 //删除
187 string strBack = strPath + fiTemp.Name;
188 strBack = strBack.Replace("//", "");
189 td = new TableCell();
190 td.Text = "<a href=# οnclick=/"javascript:delfile('" + strBack + "')/">删除</a>";
191 td.HorizontalAlign = HorizontalAlign.Center;
192 tr.Cells.Add(td);
193 //移动或重命名
194 td = new TableCell();
195 td.Text = "<a href=# οnclick=/"javascript:movefile('" + strBack + "')/">移动/重命名</a>";
196 td.HorizontalAlign = HorizontalAlign.Center;
197 tr.Cells.Add(td);
198 this.Table1.Rows.Add(tr);
199
200 }
201 }
202 catch (Exception ex)
203 {
204 this.Label2.Text = "<br><p align=center>" + ex.Message + "</p>";
205 }
206 }
207
208}
http://www.cnblogs.com/araneid/articles/1273330.html