欢迎访问我的博客 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 Default5 : System.Web.UI.Page
{
   
    private FileStream fs;
    private StreamWriter sw;
    private DirectoryInfo di;
    private FileInfo fi;
    protected void Page_Load(object sender, EventArgs e)
    {
       string fpath = Server.UrlDecode(Request.QueryString["url"]);
       string fname = Server.UrlDecode(Request.QueryString["fname"]);
       string ax = Server.UrlDecode(Request.QueryString["ax"]);
       Session["fpath"] = fpath;
       Session["lastfpath"] = Directory.GetParent(fpath).FullName;
        if (!IsPostBack)
        {         
            switch (ax)
            {
                case "editfile":
                    Panel1.Visible = true;
                    editfile(fpath, fname);
                    break;
                case "editdir":
                    Panel2.Visible = true;
                    editdir(fname);
                    break;
                case "deletedir":
                    Panel3.Visible = true;
                    deletedir(fname);
                    break;
                case "deletefile":
                    Panel4.Visible = true;
                    deletefile(fname);
                    break;              
                case "movefile":
                    Panel5.Visible = true;
                    movefile(fname,fpath);
                    break;
                case "movedir":
                    Panel6.Visible = true;
                    movedir(fname,fpath);
                    break;
                case "copyfile":
                    Panel7.Visible = true;
                    copyfile(fpath);
                    break;
                case "copydir":
                    Panel8.Visible = true;
                    copydir(fpath);
                    break;
            }
        }
    }
    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {

    }

    protected void editfile(string fpath,string fname)
    {
        TextBox1.Text=fname;
        TextBox2.Text = File.ReadAllText(fpath, Encoding.Default);
    }


    protected void Button1_Click(object sender, EventArgs e)
    {

        fs = new FileStream(Session["fpath"].ToString(), FileMode.Create, FileAccess.Write);
        sw = new StreamWriter(fs, Encoding.Default);
        sw.WriteLine(TextBox2.Text);
        sw.Close();
        fs.Close();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default4.aspx?fpath="+Session["lastfpath"].ToString());
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        di = new DirectoryInfo(Session["fpath"].ToString());
        string newpath = Session["lastfpath"].ToString() + "\\" + TextBox3.Text;
        di.MoveTo(newpath);
    }
    protected void editdir(string fname)
    {
        Label1.Text = fname;
    }
    protected void deletedir(string fname)
    {
        Label2.Text = fname;
    }
    protected void deletefile(string fname)
    {
        Label3.Text = fname;
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        di = new DirectoryInfo(Session["fpath"].ToString());
        di.Delete();
        Response.Write("<script>alert('成功删除')</script>");
        Response.Redirect("Default4.aspx?fpath=" + Session["lastfpath"].ToString());
    }
    protected void Button7_Click(object sender, EventArgs e)
    {
        fi = new FileInfo(Session["fpath"].ToString());
        fi.Delete();
        Response.Write("<script>alert('成功删除')</script>");
        Response.Redirect("Default4.aspx?fpath=" + Session["lastfpath"].ToString());
    }
    protected void movefile(string fname,string fpath)
    {
        Label5.Text = fname;
        Label4.Text = Session["lastfpath"].ToString();
        TextBox4.Text = Session["lastfpath"].ToString();
    }
    protected void movedir(string fname, string fpath)
    {
        Label6.Text = fname;
        Label7.Text = Session["lastfpath"].ToString();
        TextBox5.Text = Session["lastfpath"].ToString();
    }
    protected void Button9_Click(object sender, EventArgs e)
    {
        fi = new FileInfo(Session["fpath"].ToString());
        string newfpath =TextBox4.Text+";
        fi.MoveTo(newfpath);
        Response.Redirect("Default4.aspx?fpath=" +TextBox4.Text);
    }
    protected void Button11_Click(object sender, EventArgs e)
    {
        di = new DirectoryInfo(Session["fpath"].ToString());
        string newfpath =TextBox5.Text+";
        di.MoveTo(newfpath);
        Response.Redirect("Default4.aspx?fpath=" +TextBox5.Text);
    }
    protected void copyfile(string fpath)
    {
        Label8.Text = fpath;
        TextBox6.Text = fpath;
    }

    protected void Button13_Click(object sender, EventArgs e)
    {
        fi = new FileInfo(Label8.Text);
        fi.CopyTo(TextBox6.Text);
        Response.Redirect("Default4.aspx?fpath=" + TextBox6.Text.Substring(0,TextBox6.Text.LastIndexOf("));
    }

    protected void copydir(string fpath)
    {
        Label9.Text = fpath;
        TextBox7.Text = fpath;
    }

    protected void Button15_Click(object sender, EventArgs e)
    {
         dirCopy(Label9.Text,TextBox7.Text);     
         Response.Redirect("Default4.aspx?fpath="+TextBox7.Text.Substring(0,TextBox7.Text.LastIndexOf("));
    }
    protected void dirCopy(string oldpath,string newpath)
    {
         di = new DirectoryInfo(oldpath);
        foreach(FileSystemInfo fsi in di.GetFileSystemInfos())
        {
            if(fsi is FileInfo)
            {
                fi = (FileInfo)fsi;
                if(!Directory.Exists(newpath))
                {
                   DirectoryInfo newDir= Directory.CreateDirectory(newpath);
                   fi.CopyTo(newDir.FullName+");
                }            
                else
               {
                   fi.CopyTo(newpath+");
               }
            }
            else
            {
                DirectoryInfo child_di=(DirectoryInfo)fsi;
                string olddir=child_di.FullName;
                string dirname=child_di.FullName.Substring(child_di.FullName.LastIndexOf(");
                string newchildpath=Path.Combine(newpath,dirname);
                if(!Directory.Exists(olddir))
                    Directory.CreateDirectory(olddir);
                dirCopy(olddir,newchildpath);
            }

       }
    }
}

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