`
ljl_xyf
  • 浏览: 618652 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • c#
阅读更多

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

       }
    }
}

分享到:
评论

相关推荐

    小型文件管理系统 V1.0.0.0

    本软件集合了批量重复文件的扫描及清除,拷贝、移动、删除文件,清除空文件夹,批量文件重命名,批量文本文件合并及批量文本文件字符替换诸多功能,支持通过不同扩展名扫描得到相应的文件,再进行相应的处理,合并...

    国家开放大学计算机应用基础win7知识点--.pdf

    资源管理器中文件夹与文件的使用及管理: 在考生文件夹下创 建新的文件夹及文件(如TXT 文件及 BMP 文件) 、移动与复制文 件及文件夹、删除文件或文件夹、文件或文件夹重命名;磁盘驱 动器的改名。P65 2. 查找...

    vss如何使用(图解)

    当你要编辑或修改某个文档时,必须对文档实施check out 操作(详见3.3.5修改和编辑文件),VSS将该文档从项目中拷贝出来,放入你的工作文件夹。当你修改完毕并check in 文件之后,VSS又将文件重新拷贝到数据库中以...

    visual source safe 教程

    当你要编辑或修改某个文档时,必须对文档实施check out 操作(详见3.3.5修改和编辑文件),VSS将该文档从项目中拷贝出来,放入你的工作文件夹。当你修改完毕并check in 文件之后,VSS又将文件重新拷贝到数据库中以...

    Visual stdio source safe 教程

    当你要编辑或修改某个文档时,必须对文档实施check out 操作(详见3.3.5修改和编辑文件),VSS将该文档从项目中拷贝出来,放入你的工作文件夹。当你修改完毕并check in 文件之后,VSS又将文件重新拷贝到数据库中以...

    cmd操作命令和linux命令大全收集

    move 盘符路径要移动的文件名 存放移动文件的路径移动后文件名 移动文件,用参数/y将取消确认移动目录存在相同文件的提示就直接覆盖 fc one.txt two.txt &gt; 3st.txt 对比二个文件并把不同之处输出到3st.txt文件中...

    TransMac_11_4

    TransMac 11破解版是一款可以读、写和格式化Macintosh高密度磁盘、CD-ROM以及SCSI光驱(包括SyQuest、Bernoulli、Zip、...当然,它允许用户执行的操作,包括读写数据,而且复制、重命名和删除文件和文件夹就像普通磁盘上。

    WinRAR_4.0.exe

    RAR 是一个强力压缩工具,允许你管理和操作压缩文件。控制台 RAR 只支持 RAR 格式,带有的 ".rar" 扩展名的文件。ZIP 和其他格式不被支持。Windows 用户可以 安装图形界面 RAR 版本 - WinRAR,它可以处理更多的...

    入门学习Linux常用必会60个命令实例详解doc/txt

    Linux提供了大量的命令,利用它可以有效地完成大量的工作,如磁盘操作、文件存取、目录操作、进程管理、文件权限设定等。所以,在Linux系统上工作离不开使用系统提供的命令。要想真正理解Linux系统,就必须从Linux...

Global site tag (gtag.js) - Google Analytics