<%@ Page language="c#" Codebehind="Default.aspx.cs" AutoEventWireup="false" Inherits="FileManager.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>::AspxHome::</title>
<style type="text/css">.ButtonStyle { BORDER-RIGHT: thin solid; BORDER-TOP: thin solid; BORDER-LEFT: thin solid; COLOR: buttontext; BORDER-BOTTOM: thin solid; BACKGROUND-COLOR: buttonshadow }
</style>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<script language="javascript">
function inputtext(pmsg,validstr) //输入文本
{
dir = prompt(pmsg,validstr);
if (dir != null)
{
document.Form1.text.value=dir;
document.Form1.submit();
return true;
}
}
function inputyesno(pmsg) //选择是否
{
dir = confirm(pmsg);
if (dir ==true)
{
document.Form1.text.value="true";
document.Form1.submit();
return true;
}
}
function lboData_click()//listbox的单击
{
document.Form1.item.value = document.Form1.lboData.value;
document.Form1.text.value = "click";
}
function lboData_dblclick()//listbox的双击
{
document.all("btnOpenDir").click();
}
function btnEdit_click()//edit的单击
{
document.all("btnEdit").click();
}
</script>
</HEAD>
<body bgColor="#6699cc" MS_POSITIONING="GridLayout">
<form id="Form1" method="post" encType="multipart/form-data" runat="server">
<asp:button id="btnCreateDir" style="Z-INDEX: 101; LEFT: 412px; POSITION: absolute; TOP: 98px" runat="server" Width="53" Height="28" Text="新建目录" CssClass="ButtonStyle"></asp:button>
<asp:label id="labErrorMessage" style="Z-INDEX: 112; LEFT: 20px; POSITION: absolute; TOP: 371px" runat="server" Width="674px" Height="26px" ForeColor="Red" Font-Size="15pt" Font-Bold="True"></asp:label><asp:button id="btnDelete" style="Z-INDEX: 102; LEFT: 412px; POSITION: absolute; TOP: 282px" runat="server" Width="53px" Height="28px" Text="删除" CssClass="ButtonStyle"></asp:button><asp:button id="btnCreateFile" style="Z-INDEX: 103; LEFT: 412px; POSITION: absolute; TOP: 331px" runat="server" Width="53px" Height="28px" Text="新建文件" CssClass="ButtonStyle"></asp:button><asp:button id="btnOpenDir" style="Z-INDEX: 104; LEFT: 412px; POSITION: absolute; TOP: 52px" runat="server" Width="53px" Height="28px" Text="打开目录" CssClass="ButtonStyle"></asp:button><asp:button id="btnEdit" style="Z-INDEX: 105; LEFT: 412px; POSITION: absolute; TOP: 190px" runat="server" Width="53px" Height="28px" Text="编辑" CssClass="ButtonStyle"></asp:button><asp:button id="btnDownFile" style="Z-INDEX: 106; LEFT: 412px; POSITION: absolute; TOP: 236px" runat="server" Width="53px" Height="28px" Text="下载文件" CssClass="ButtonStyle"></asp:button><asp:button id="btnRename" style="Z-INDEX: 107; LEFT: 412px; POSITION: absolute; TOP: 144px" runat="server" Width="53" Height="28" Text="更名" CssClass="ButtonStyle"></asp:button><asp:listbox id="lboData" style="Z-INDEX: 108; LEFT: 27px; POSITION: absolute; TOP: 51px" runat="server" Width="373px" Height="310px" ForeColor="White" BackColor="Black"></asp:listbox><asp:label id="labCurrentDir" style="Z-INDEX: 109; LEFT: 82px; POSITION: absolute; TOP: 20px" runat="server" Width="596px" Height="24px"></asp:label><INPUT style="Z-INDEX: 110; LEFT: 212px; WIDTH: 163px; POSITION: absolute; TOP: 234px; HEIGHT: 26px" type="hidden" size="21" name="text">
<INPUT style="Z-INDEX: 111; LEFT: 212px; WIDTH: 165px; POSITION: absolute; TOP: 269px; HEIGHT: 26px" type="hidden" size="22" name="item">
<asp:button id="btnUpload" style="Z-INDEX: 113; LEFT: 474px; POSITION: absolute; TOP: 48px" runat="server" Width="62px" Height="29px" Text="上载" CssClass="ButtonStyle"></asp:button><asp:button id="btnUp" style="Z-INDEX: 114; LEFT: 29px; POSITION: absolute; TOP: 18px" runat="server" Width="49px" Height="26px" Text="向上" CssClass="ButtonStyle"></asp:button><asp:dropdownlist id="FileCount" style="Z-INDEX: 115; LEFT: 551px; POSITION: absolute; TOP: 51px" runat="server" Width="87px" Height="81px" CssClass="ButtonStyle" AutoPostBack="True">
<asp:ListItem Value="1" Selected="True">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
<asp:ListItem Value="7">7</asp:ListItem>
<asp:ListItem Value="8">8</asp:ListItem>
<asp:ListItem Value="9">9</asp:ListItem>
<asp:ListItem Value="10">10</asp:ListItem>
</asp:dropdownlist><asp:table id="TableFile" style="Z-INDEX: 116; LEFT: 477px; POSITION: absolute; TOP: 83px" runat="server" Width="242px" Height="24px" CellPadding="0" CellSpacing="0"></asp:table></form>
</body>
</HTML>
后台代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Security;
namespace FileManager
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnCreateDir;
protected System.Web.UI.WebControls.Button btnDelete;
protected System.Web.UI.WebControls.Button btnOpenDir;
protected System.Web.UI.WebControls.Button btnEdit;
protected System.Web.UI.WebControls.Button btnDownFile;
protected System.Web.UI.WebControls.ListBox lboData;
protected System.Web.UI.WebControls.Label labCurrentDir;
protected System.Web.UI.WebControls.Label labErrorMessage;
protected System.Web.UI.WebControls.Button btnUpload;
protected System.Web.UI.WebControls.Button btnUp;
protected System.Web.UI.WebControls.Button btnCreateFile;
protected System.Web.UI.WebControls.Table TableFile;
protected System.Web.UI.WebControls.DropDownList FileCount;
protected System.Web.UI.WebControls.Button btnRename;
public static string topdir;//最上层目录
void GetFolders(Object sender,EventArgs e,string CurrentFolder)
{
System.IO.DirectoryInfo di=new DirectoryInfo(CurrentFolder);
DirectoryInfo[] diArr;
FileInfo[] fiArr;
try
{
diArr = di.GetDirectories();
fiArr = di.GetFiles();
}
catch(Exception ex)//空引用
{
labErrorMessage.Text=ex.Message;
return;
}
string strTmp;
//设置当前目录
labCurrentDir.Text=CurrentFolder;
lboData.Items.Clear();
// print out the names of the directories
foreach (DirectoryInfo dri in diArr) //显示目录
{
strTmp=dri.Name;
strTmp+=" <目录>";
lboData.Items.Add(new ListItem(strTmp,dri.FullName.ToLower()));
}
foreach (FileInfo fi in fiArr) //显示文件信息
{
strTmp=fi.Name;
strTmp+=" "+fi.Length.ToString();
strTmp+=" "+fi.LastAccessTime.ToString();
lboData.Items.Add(new ListItem(strTmp,fi.FullName.ToLower()));
}
labErrorMessage.Text="";
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
DataBind();
this.btnCreateDir.Attributes["onclick"]="javascript:return inputtext('输入一个新的路径名','请输入一个有效的路径名')";
this.btnRename.Attributes["onclick"]="javascript:return inputtext('输入一个新的名称不要包含路径名,如果修改后的文件或目录存在将被新的修改过的文件或目录覆盖!','请输入一个有效的名称')";
this.btnDelete.Attributes["onclick"]="javascript:return inputyesno('你确认删除这个文件吗?!')";
this.btnCreateFile.Attributes["onclick"]="javascript:return inputtext('输入新文件名','请输入一个有效的文件名')";
this.btnEdit.Attributes["onclick"]="btnEdit_click()";
this.lboData.Attributes["onclick"]="lboData_click()";
this.lboData.Attributes["ondblclick"]="lboData_dblclick()";
lboData.Items.Clear();
string strCurPath=Server.MapPath("test");
if(strCurPath.LastIndexOf("\\")==strCurPath.Length)
{
strCurPath.Remove(strCurPath.Length,1);
}
topdir=strCurPath;
GetFolders(null,e,strCurPath);
}
this.FileCount_SelectedIndexChanged(sender,e);
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnCreateDir.Click += new System.EventHandler(this.btnCreateDir_Click);
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
this.btnCreateFile.Click += new System.EventHandler(this.btnCreateFile_Click);
this.btnOpenDir.Click += new System.EventHandler(this.btnOpenDir_Click);
this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);
this.btnDownFile.Click += new System.EventHandler(this.btnDownFile_Click);
this.btnRename.Click += new System.EventHandler(this.btnRename_Click);
this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
this.btnUp.Click += new System.EventHandler(this.btnUp_Click);
this.FileCount.SelectedIndexChanged += new System.EventHandler(this.FileCount_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnUp_Click(object sender, System.EventArgs e)
{
//回到上一级目录
string strCurrentDir=labCurrentDir.Text.ToLower();
if(strCurrentDir.CompareTo(topdir.ToLower())==0)
{
labErrorMessage.Text="×没有访问此目录的权限!";
return;
}
try
{
DirectoryInfo di = System.IO.Directory.GetParent(strCurrentDir);
GetFolders(sender,e,di.FullName);
}
catch (Exception ex)
{
labErrorMessage.Text = ex.Message.ToString();
return;
}
}
private void btnOpenDir_Click(object sender, System.EventArgs e)
{
if (lboData.SelectedIndex > -1)
{
try
{
DirectoryInfo di = new DirectoryInfo(lboData.SelectedItem.Value);
if (di.Exists == true)
{
GetFolders(sender, e, lboData.SelectedItem.Value);
}
}
catch (Exception ex)//空引用
{
labErrorMessage.Text = ex.Message;
return;
}
}
}
private void btnCreateDir_Click(object sender, System.EventArgs e)
{
try
{
string strNewDir = Request.Form.Get("text");
string strCurDir = this.labCurrentDir.Text;
if (strNewDir.Length > 0)
{
DirectoryInfo di = new DirectoryInfo(strCurDir + "\\" + strNewDir);
if (di.Exists == true)
{
this.labErrorMessage.Text = "×您要新建的目录已经存在!";
}
else
{
di.Create();
GetFolders(sender, e, strCurDir);
return;
}
}
}
catch (Exception ex)//空引用
{
labErrorMessage.Text = ex.Message;
return;
}
}
private void btnRename_Click(object sender, System.EventArgs e)
{
if (lboData.SelectedIndex > -1)
{
try
{
DirectoryInfo di = new DirectoryInfo(lboData.SelectedItem.Value);
FileInfo fi = new FileInfo(lboData.SelectedItem.Value);
if (di.Exists == true)//目录
{
di.MoveTo(this.labCurrentDir.Text + "\\" + Request.Form.Get("text"));
}
if (fi.Exists == true)//文件
{
fi.MoveTo(this.labCurrentDir.Text + "\\" + Request.Form.Get("text"));
}
GetFolders(sender, e, this.labCurrentDir.Text);
}
catch (Exception ex)//空引用
{
labErrorMessage.Text = ex.Message;
return;
}
}
else
{
this.labErrorMessage.Text = "×您没有选择要修改名字的文件或目录!";
}
}
private void btnDelete_Click(object sender, System.EventArgs e)
{
if (lboData.SelectedIndex > -1)
{
try
{
if (Request.Form.Get("text") == "true")
{
DirectoryInfo di = new DirectoryInfo(lboData.SelectedItem.Value);
FileInfo fi = new FileInfo(lboData.SelectedItem.Value);
if (di.Exists == true)//目录
{
di.Delete(true);
}
if (fi.Exists == true)//文件
{
fi.Delete();
}
GetFolders(sender, e, this.labCurrentDir.Text);
}
}
catch (Exception ex)//空引用
{
labErrorMessage.Text = ex.Message;
return;
}
}
else
{
this.labErrorMessage.Text = "×您没有选择要删除的文件或目录!";
}
}
private void btnCreateFile_Click(object sender, System.EventArgs e)
{
try
{
string strNewFile = Request.Form.Get("text");
string strCurDir = this.labCurrentDir.Text;
if (strNewFile.Length > 0)
{
if (File.Exists(strCurDir + "\\" + strNewFile) == true)
{
this.labErrorMessage.Text = "×您要新建的文件已经存在!";
}
else
{
File.Create(strCurDir + "\\" + strNewFile);
GetFolders(sender, e, strCurDir);
return;
}
}
}
catch (Exception ex)//调用方没有所需的权限。
{
this.labErrorMessage.Text = ex.Message;
return;
}
}
private void btnEdit_Click(object sender, System.EventArgs e)
{
try
{
if (this.lboData.SelectedIndex > -1)
{
string filename = this.lboData.SelectedItem.Value.ToString();
FileInfo fi = new FileInfo(filename);
if (fi.Exists == true)
{
switch (fi.Extension)
{
case ".txt":
case ".aspx":
case ".cs":
case ".html":
case ".htm":
case ".asp":
case ".css":
case ".config":
case ".asax":
//打开文件并编辑
if (filename.Length != 0)
{
string url = Server.UrlEncode(fi.FullName);
Response.Write("<script language='javascript'>");
Response.Write("window.open('FormEdit.aspx?FileName=" + url + "','FileEdit','toolbar=no,location=no,width=650,height=600,left=80');");
Response.Write("</script>");
}
break;
default:
this.labErrorMessage.Text = "您选择的文件类型不能编辑!";
return;
}
}
}//if(this.lboData.SelectedIndex>-1)
else
{
this.labErrorMessage.Text = "请先选择要编辑的文件!";
return;
}//else
}//try
catch (Exception ex)
{
this.labErrorMessage.Text = ex.Message;
return;
}
}
private void btnDownFile_Click(object sender, System.EventArgs e)
{
try
{
DirectoryInfo di = new DirectoryInfo(lboData.SelectedItem.Value);
FileInfo fi = new FileInfo(lboData.SelectedItem.Value);
if (di.Exists == true)//目录
{
this.labErrorMessage.Text = "目录不能下载!";
}
if (fi.Exists == true)//文件
{
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fi.FullName, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", fi.Length.ToString());
Response.WriteFile(fi.FullName);
Response.Flush();
Response.End();
}
}
catch (Exception ex)
{
this.labErrorMessage.Text = ex.Message;
}
}
private void btnUpload_Click(object sender, System.EventArgs e)
{
HttpFileCollection HttpFiles = Request.Files;
try
{
for (int i = 0; i < HttpFiles.Count; i++)
{
HttpPostedFile File = HttpFiles.Get(i);
if (File.ContentLength > 0)
{
File.SaveAs(this.labCurrentDir.Text + "\\" + Path.GetFileName(File.FileName));
}
}
this.GetFolders(sender, e, this.labCurrentDir.Text);
}
catch (Exception Ex)
{
this.labErrorMessage.Text = "文件保存时出错:" + Ex.Message;
return;
}
}
private void FileCount_SelectedIndexChanged(object sender, System.EventArgs e)
{
try
{
TableFile.Controls.Clear();
int numrows = Convert.ToInt32(this.FileCount.SelectedItem.Value.ToString());
for (int j = 0; j < numrows; j++)
{
TableRow r = new TableRow();
TableCell c = new TableCell();
HtmlInputFile myFile = new HtmlInputFile();
myFile.Size = 35;
myFile.Style.Add("CssClass", "ButtonStyle");
r.Height = 32;
c.Controls.Add(myFile);
r.Cells.Add(c);
TableFile.Rows.Add(r);
}
}
catch (Exception ex)
{
this.labErrorMessage.Text = ex.Message.ToString();
}
}//btnUpload_Click
}
}