简易的文件磁盘管理操作2(文件、文件夹的编辑创建删除移动拷贝重命名)_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 简易的文件磁盘管理操作2(文件、文件夹的编辑创建删除移动拷贝重命名)

简易的文件磁盘管理操作2(文件、文件夹的编辑创建删除移动拷贝重命名)

 2010/11/16 11:45:58  ljl_xyf  http://ljl-xyf.javaeye.com  我要评论(0)
  • 摘要:usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls
  • 标签:文件磁盘管理操作

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.Text;

public partial class Default4 : System.Web.UI.Page
{
?? //DropDownList1的AppendDataBoundsItems属性要改成true
?? string fpath;
?? FileStream fs;
?? DirectoryInfo di;
?? FileInfo fi;
??? protected void Page_Load(object sender, EventArgs e)
??? {
??????? if (!IsPostBack)
??????? {
??????????? if (Request.QueryString["fpath"] == null)
??????????? {
??????????????? fpath = Server.MapPath(".");
??????????????? Label1.Text = fpath;
??????????????? TextBox1.Text = fpath;
??????????????? fill(fpath);
??????????? }
??????????? else
??????????? {
??????????????? fpath = Server.UrlDecode(Request.QueryString["fpath"]);
??????????????? Label1.Text = fpath;
??????????????? TextBox1.Text = fpath;
??????????????? fill(fpath);
??????????? }
??????? }
??? }

// http://www.my400800.cn

??? protected void fill(string fpath)
??? {
???????
??????????? TableCell td;
??????????? TableRow tr;
??????????? Button btn;

??????????? tr = new TableRow();

??????????? td = new TableCell();
??????????? btn = new Button();
??????????? btn.Text = "名称";
??????????? btn.Width = 200;
??????????? btn.Attributes.Add("onClick", "return false");
??????????? td.Controls.Add(btn);
??????????? tr.Cells.Add(td);


??????????? td = new TableCell();
??????????? btn = new Button();
??????????? btn.Text = "类型";
??????????? btn.Width = 60;
??????????? btn.Attributes.Add("onClick", "return false");
??????????? td.Controls.Add(btn);
??????????? tr.Cells.Add(td);

??????????? td = new TableCell();
??????????? btn = new Button();
??????????? btn.Text = "大小";
??????????? btn.Width = 80;
??????????? btn.Attributes.Add("onClick", "return false");
??????????? td.Controls.Add(btn);
??????????? tr.Cells.Add(td);

??????????? td = new TableCell();
??????????? btn = new Button();
??????????? btn.Text = "修改日期";
??????????? btn.Width = 150;
??????????? btn.Attributes.Add("onClick", "return false");
??????????? td.Controls.Add(btn);
??????????? tr.Cells.Add(td);


??????????? td = new TableCell();
??????????? btn = new Button();
??????????? btn.Text = "操作";
??????????? btn.Width = 200;
??????????? btn.Attributes.Add("onClick", "return false");
??????????? td.Controls.Add(btn);
??????????? tr.Cells.Add(td);

??????????? Table1.Rows.Add(tr);

??????????? string fname;
??????????? string fexp;
??????????? string fsize;
??????????? string ftime;
??????????? HyperLink href;
??????????? string url;

??????????? di = new DirectoryInfo(fpath);
??????????? foreach (FileSystemInfo fsi in di.GetFileSystemInfos())
??????????? {
??????????????? if (fsi is FileInfo)
??????????????? {
??????????????????? FileInfo fi = (FileInfo)fsi;
??????????????????? fname = fi.Name;
??????????????????? if (fi.Extension.Length < 1)
??????????????????????? fexp = "";
??????????????????? else
??????????????????????? fexp = fi.Extension.Remove(0, 1);
??????????????????? fsize = fi.Length.ToString();
??????????????????? ftime = fi.LastWriteTime.ToString();
??????????????? }
??????????????? else
??????????????? {
??????????????????? di = (DirectoryInfo)fsi;
??????????????????? fname = di.Name;
??????????????????? fexp = "文件夹";
??????????????????? 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(fexp));
??????????????? 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)
??????????????? {
??????????????????? href = new HyperLink();
??????????????????? href.Text = "打开";
??????????????????? url = Server.UrlEncode(fpath);
??????????????????? href.NavigateUrl = "~/Default4.aspx?fpath=" + url + "\\" + fname;
??????????????????? td = new TableCell();
??????????????????? td.Controls.Add(href);
??????????????????? tr.Cells.Add(td);
??????????????? }
??????????????? else
??????????????? {
??????????????????? td = new TableCell();
??????????????? }
??????????????? if (fsi is FileInfo)
??????????????? {
??????????????????? href = new HyperLink();
??????????????????? href.Text = "编辑";
??????????????????? url =Server.UrlEncode(fpath)+ "\\" + fname;
??????????????????? href.NavigateUrl = "~/Default5.aspx?ax=editfile&url="+url+"&fname=" + fname;
??????????????????? td.Controls.Add(href);
??????????????? }
??????????????? else
??????????????? {
??????????????????? href = new HyperLink();
??????????????????? href.Text = "重命名";
??????????????????? url =Server.UrlEncode(fpath)+ "\\" + fname;
??????????????????? href.NavigateUrl = "~/Default5.aspx?ax=editdir&url=" + url + "&fname=" + fname;
??????????????????? td.Controls.Add(href);
??????????????? }
??????????????? if (fsi is FileInfo)
??????????????? {
??????????????????? href = new HyperLink();
??????????????????? href.Text = "删除";
??????????????????? href.Attributes.Add("onClick", "return confirm('一旦删除,无法恢复,回收站也没有')");
??????????????????? url =Server.UrlEncode(fpath)+ "\\" + fname;
??????????????????? href.NavigateUrl = "~/Default5.aspx?ax=deletefile&url=" + url + "&fname=" + fname;
??????????????????? td.Controls.Add(href);
??????????????? }
??????????????? else
??????????????? {
??????????????????? href = new HyperLink();
??????????????????? href.Text = "删除";
??????????????????? href.Attributes.Add("onClick", "return confirm('一旦删除,无法恢复,回收站也没有')");
??????????????????? url =Server.UrlEncode(fpath)+ "\\" + fname;
??????????????????? href.NavigateUrl = "~/Default5.aspx?ax=deletedir&url=" + url + "&fname=" + fname;
??????????????????? td.Controls.Add(href);
??????????????? }
??????????????? if (fsi is FileInfo)
??????????????? {
??????????????????? href = new HyperLink();
??????????????????? href.Text = "移动";
??????????????????? url =Server.UrlEncode(fpath)+ "\\" + fname;
??????????????????? href.NavigateUrl = "~/Default5.aspx?ax=movefile&url=" + url + "&fname=" + fname;
??????????????????? td.Controls.Add(href);
??????????????? }
??????????????? else
??????????????? {
??????????????????? href = new HyperLink();
??????????????????? href.Text = "移动";
??????????????????? url =Server.UrlEncode(fpath)+ "\\" + fname;
??????????????????? href.NavigateUrl = "~/Default5.aspx?ax=movedir&url=" + url + "&fname=" + fname;
??????????????????? td.Controls.Add(href);
??????????????? }

??????????????? if (fsi is FileInfo)
??????????????? {
??????????????????? href = new HyperLink();
??????????????????? href.Text = "拷贝";
??????????????????? url =Server.UrlEncode(fpath)+ "\\" + fname;
??????????????????? href.NavigateUrl = "~/Default5.aspx?ax=copyfile&url=" + url + "&fname=" + fname;
??????????????????? td.Controls.Add(href);
??????????????? }
??????????????? else
??????????????? {
??????????????????? href = new HyperLink();
??????????????????? href.Text = "拷贝";
??????????????????? url =Server.UrlEncode(fpath)+ "\\" + fname;
??????????????????? href.NavigateUrl = "~/Default5.aspx?ax=copydir&url=" + url + "&fname=" + fname;
??????????????????? td.Controls.Add(href);
??????????????? }

??????????????? tr.Cells.Add(td);

??????????????? Table1.Rows.Add(tr);
??????????? }

??????????? //DropDownList1的AppendDataBoundsItems属性要改成true才能添加静态项
??????????? DropDownList1.Items.Clear();
??????????? DropDownList1.Items.Add("请选择目标目录");
??????????? if (fpath.Length > 4)
??????????? {
??????????????? DropDownList1.Items.Add("返回上级目录");
??????????????? DropDownList1.DataSource = Directory.GetDirectories(fpath);
??????????????? DropDownList1.DataBind();
??????????? }
??????????? else
??????????? {
??????????????? string[] item = Directory.GetLogicalDrives();
??????????????? for (int i = 0; i < item.Length; i++)
??????????????? {
??????????????????? DropDownList1.Items.Add(item[i]);
??????????????? }
??????????????? DropDownList1.DataSource = Directory.GetDirectories(fpath);
??????????????? DropDownList1.DataBind();
??????????? }
??? }

??? protected void Button1_Click(object sender, EventArgs e)
??? {
??????? fpath = TextBox1.Text;
??????? Label1.Text = fpath;
??????? fill(fpath);
??? }
??? protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
??? {
??????? switch (DropDownList1.SelectedItem.Text)
??????? {
??????????? case "请选择目标目录":
??????????????? break;
??????????? case "返回上级目录":
??????????????? string rootDir = Directory.GetParent(TextBox1.Text).FullName;
??????????????? fpath = rootDir;
??????????????? TextBox1.Text = fpath;
??????????????? Label1.Text = fpath;
??????????????? fill(fpath);
??????????????? break;
??????????? default:
??????????????? fpath = DropDownList1.SelectedItem.Text;
??????????????? TextBox1.Text = fpath;
??????????????? Label1.Text = fpath;
??????????????? fill(fpath);
??????????????? break;
??????? }

??? }
??? protected void Button2_Click(object sender, EventArgs e)
??? {
??????? string newpath = Label1.Text + "\\" + TextBox2.Text;
??????? creatFile(newpath);
??????? fpath = Label1.Text;
??????? fill(fpath);
??? }
??? protected void creatFile(string path)
??? {
??????? try
??????? {
?????????? fi=new FileInfo(path);
?????????? if (!fi.Exists)
?????????????? fi.Create();
?????????? else
?????????????? Response.Write("<script>alert('文件已经存在')</script>");
??????? }
??????? catch (Exception ex)
??????? {
??????????? Response.Write("<script>alert('" + ex.Message + "')</script>");
??????? }
??? }
??? protected void Button3_Click(object sender, EventArgs e)
??? {
??????? string newpath = Label1.Text + "\\" + TextBox3.Text;
??????? creatDir(newpath);
??????? fpath = Label1.Text;
??????? fill(fpath);
??? }
??? protected void creatDir(string path)
??? {
??????? try
??????? {
??????????? di = new DirectoryInfo(path);
??????????? if (!di.Exists)
??????????????? di.Create();
??????????? else
??????????????? Response.Write("<script>alert('文件夹已经存在')</script>");
??????? }
??????? catch (Exception ex)
??????? {
??????????? Response.Write("<script>alert('" + ex.Message + "')</script>");
??????? }
??? }
}

  • 相关文章
发表评论
用户名: 匿名