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

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

//http://www.my400800.cn
    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+"\\"+Label5.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+"\\"+Label6.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("\\")+1 ));
    }

    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("\\")+1 ));
    }
    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+"\\"+fi.Name );
                }            
                else
               {
                   fi.CopyTo(newpath+"\\"+fi.Name );
               }
            }
            else
            {
                DirectoryInfo child_di=(DirectoryInfo)fsi;
                string olddir=child_di.FullName;
                string dirname=child_di.FullName.Substring(child_di.FullName.LastIndexOf("\\")+1 );
                string newchildpath=Path.Combine(newpath,dirname);
                if(!Directory.Exists(olddir))
                    Directory.CreateDirectory(olddir);
                dirCopy(olddir,newchildpath);
            }

       }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值