欢迎访问我的博客 https://javascript.shop

net8:简易的文件磁盘管理操作一(包括文件以及文件夹的编辑创建删除移动拷贝重命名等)

原文发布时间为:2008-08-07 —— 来源于本人的百度文章 [由搬家工具导入]

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);
            }
        }
    }

    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>");
        }
    }
}

----------------------------------------------------

posted @ 2017-07-09 15:04  孑孓子  阅读(168)  评论(0编辑  收藏  举报
欢迎访问我的博客 https://javascript.shop