📄 webform1.aspx.cs
字号:
using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication17
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlTableCell td1;
protected System.Web.UI.HtmlControls.HtmlInputHidden cmd;
protected System.Web.UI.HtmlControls.HtmlInputHidden name1;
protected System.Web.UI.WebControls.Label Label1;
public string Expand_Dir;
private void Page_Load(object sender, System.EventArgs e)
{
string ServerDir = MapPath(".")+"\\";
this.Expand_Dir= "";
switch(this.cmd.Value)
{
case(""):
break;
case("open"): //打开
string[]open_tmp=this.name1.Value.Split(new char[]{'*'});
foreach(string open_tmp2 in open_tmp)
{
if(Directory.Exists(ServerDir+open_tmp2))
{
this.AddDirState(open_tmp2);
}
else if(File.Exists(ServerDir+open_tmp2))
{
Response.Redirect("http://"+Request.UrlReferrer.Host+ResolveUrl(open_tmp2));
}
}
break;
case("newdir"): //新建文件夹
string [] newdir_tmp = this.name1.Value.Split(new Char[]{'*'});
if(newdir_tmp.Length==2) //只包含“新建文件夹”及其父目录
{
if(newdir_tmp[0]=="")
{
newdir_tmp[0]="personal";
}
if(this.CheckName(newdir_tmp[1])&&Directory.Exists(ServerDir+newdir_tmp[0]))
{
string newdir_tmp2=ServerDir+newdir_tmp[0]+"\\"+newdir_tmp[1];
this.NameExisted(1,newdir_tmp2,out newdir_tmp2);
Directory.CreateDirectory(newdir_tmp2);
this.AddDirState( newdir_tmp[0]);
}
}
break;
case("expand_dir"): //展开文件夹
this.Expand_Dir =this.name1.Value;
break;
case("zip"): // 压缩
string[] zip_tmp = this.name1.Value.Split(new char[]{'*'});
string zip_tmp1= zip_tmp[zip_tmp.Length-1];
if(this.CheckName(zip_tmp1)) //检测文件命名的合法性
{
string zip_tmp2=zip_tmp1.Replace(".zip","");
//移除“.zip”后缀,并防止名称重复
this.NameExisted(1,ServerDir+@"personal\"+zip_tmp2+".zip",out zip_tmp2);
StreamWriter zip_sr =File.CreateText(ServerDir+"list.dat");
zip_sr.Write("");
zip_sr.Close();
for(int m =0;m<zip_tmp.Length-1;m++)
{
//以系统默认的 ANSI 编码创建压缩文件列表
StreamWriter zip_sw2 =new StreamWriter(ServerDir+"list.dat",true,System.Text.Encoding.Default);
zip_sw2.WriteLine(ServerDir+zip_tmp[m]);
zip_sw2.Close();
}
System.Diagnostics.Process Process1=new System.Diagnostics.Process();
Process1.StartInfo.FileName=ServerDir+"Winrar.exe";
Process1.StartInfo.Arguments=" a -ep1 -inul -y "+zip_tmp2+@" @"+ServerDir+"list.dat";
Process1.Start(); //进行压缩
while(!Process1.HasExited) //等待压缩的完成
{
}
}
break;
case("unzip"): //解压缩
string [] un_tmp =this.name1.Value.Split(new char[]{'*'});
int i2=0; //防止名称冲突的参数
foreach(string un_tmp2 in un_tmp)
{
if(un_tmp2!="")
{
string un_time=System.DateTime.Now.ToShortDateString()+"-"+System.DateTime.Now.Hour.ToString()+"-"+System.DateTime.Now.Minute.ToString()+"-"+(System.DateTime.Now.Second+i2).ToString();
string un_dir =ServerDir+@"personal\Unzip-"+un_time;
Directory.CreateDirectory(un_dir); //创建以解压时间为名的文件夹
StreamWriter un_sw=File.AppendText(ServerDir+"DirState.dat");
un_sw.Write(@"personal\Unzip-"+un_time+"*");
un_sw.Close();
System.Diagnostics.Process Process2=new System.Diagnostics.Process();
Process2.StartInfo.FileName=ServerDir+"Winrar.exe";
Process2.StartInfo.Arguments=" x -inul -y "+ServerDir+un_tmp2+" "+un_dir;
Process2.Start(); //进行解压
while(!Process2.HasExited) //等待解压的完成
{
}
i2++;
}
}
break;
case("upload"): //上传文件
string [] up=this.name1.Value.Split(new char []{'*'});
if(Directory.Exists(ServerDir+up[0]))
{
this.AddDirState(up[0]);
Server.Transfer("UpLoad.aspx?d="+up[0]);
}
break;
case("download"):
string[] down_tmp1=this.name1.Value.Split(new char[]{'*'});
int dl=0;
foreach(string down_tmp2 in down_tmp1)
{
if(down_tmp2!="")
{
dl++; // 检测下载文件的数目
}
}
if(dl==1&&!Directory.Exists(ServerDir+down_tmp1[0])) //单个文件的下载
{
if(File.Exists(ServerDir+down_tmp1[0]))
{
Response.Redirect("http://"+Request.UrlReferrer.Host+ResolveUrl(down_tmp1[0]),false);
}
}
else if(dl!=0) //多个文件或目录进行压缩打包下载
{
StreamWriter zip_sr =File.CreateText(ServerDir+"list.dat");
zip_sr.Write("");
zip_sr.Close();
for(int m =0;m<down_tmp1.Length;m++)
{
if(down_tmp1[m]!="")
{
//创建压缩文件列表
StreamWriter down_sw2 =new StreamWriter(ServerDir+"list.dat",true,System.Text.Encoding.Default);
down_sw2.WriteLine(ServerDir+down_tmp1[m]);
down_sw2.Close();
}
}
if(File.Exists(ServerDir+@"personal\DownLoad.zip"))
{
File.Delete(ServerDir+@"personal\DownLoad.zip"); //删除原有的 DownLoad.zip
}
string down_tmp3 =@"personal\DownLoad.zip";
System.Diagnostics.Process Process3=new System.Diagnostics.Process();
Process3.StartInfo.FileName=ServerDir+"Winrar.exe";
Process3.StartInfo.Arguments=" a -ep1 -inul -y "+ServerDir+down_tmp3+@" @"+ServerDir+"list.dat";
Process3.Start(); //启动压缩
while(!Process3.HasExited) //等待完成
{
}
Response.Redirect("http://"+Request.UrlReferrer.Host+ResolveUrl(down_tmp3));
}
break;
case("rename"):
string [] rename_tmp1 =this.name1.Value.Split(new char []{'*'});
if(rename_tmp1.Length==2) //只包含“原名”与“新名称”二项
{
if(this.CheckName(rename_tmp1[1])) //新名称是否合法
{
int rm1 =rename_tmp1[0].LastIndexOf("\\")+1;
int rm2 =rename_tmp1[0].Length-rm1;
string rename_tmp2=rename_tmp1[0].Remove(rm1,rm2);
//新名称是否重名
this.NameExisted(1,ServerDir+rename_tmp2+rename_tmp1[1],out rename_tmp2);
if(Directory.Exists(ServerDir+rename_tmp1[0]))
{
Directory.Move(ServerDir+rename_tmp1[0],rename_tmp2);
}
else if(File.Exists(ServerDir+rename_tmp1[0]))
{
File.Move(ServerDir+rename_tmp1[0],rename_tmp2);
}
}
}
break;
case("del"): //删除
string[] del_tmp1 =this.name1.Value.Split(new char []{'*'});
if(!Directory.Exists(ServerDir+@"personal\Recycler"))
{
//创建 personal\Recycler
Directory.CreateDirectory(ServerDir+@"personal\Recycler");
}
foreach(string del_s in del_tmp1)
{
if(del_s!="")
{
this.AddDirState("personal\\Recycler");
int d =del_s.LastIndexOf("\\")+1;
string del_tmp2 =del_s.Substring(d,del_s.Length-d); //获取文件名称
if(Directory.Exists(ServerDir+del_s))
{
if(del_s.IndexOf("personal\\Recycler")<0) //不在 personal\Recycler 里
{
this.NameExisted(1,ServerDir+"personal\\Recycler\\"+del_tmp2,out del_tmp2);
Directory.Move(ServerDir+del_s,del_tmp2);
}
else if(del_s!="personal\\Recycler") //保护 personal\Recycler
{
Directory.Delete(ServerDir+del_s,true);
}
}
else if(File.Exists(ServerDir+del_s))
{
if(del_s.IndexOf("personal\\Recycler")<0)
{
this.NameExisted(1,ServerDir+"personal\\Recycler\\"+del_tmp2,out del_tmp2);
File.Move(ServerDir+del_s,del_tmp2);
}
else
{
File.Delete(ServerDir+del_s);
}
}
}
}
break;
case("close"): //关闭
StreamReader close_sr=new StreamReader(ServerDir+"DirState.dat");
string close_tmp2 =close_sr.ReadToEnd();
close_sr.Close();
string [] close_tmp1=this.name1.Value.Split(new char []{'*'});
foreach(string ss in close_tmp1)
{
if(ss!="")
{
close_tmp2=close_tmp2.Replace(ss+"*","");
}
}
StreamWriter close_sw=File.CreateText(ServerDir+"DirState.dat");
close_sw.Write(close_tmp2);
close_sw.Close();
break;
case("split"): //分割
string [] sp = this.name1.Value.Split(new char[]{'*'});
string sp_file1 =ServerDir+sp[0];
int i3=0;
try
{
i3 = Int32.Parse(sp[sp.Length-1]);
}
catch
{
i3=2; //读取数字错误,则默认为 2
}
if(File.Exists(sp_file1)&&i3<=5&&i3>=2) //为减轻服务器负担,限定 i3 为 2 到 5 的整数
{
FileStream file1_fs =new FileStream(sp_file1,FileMode.Open);
int m=(int)file1_fs.Length;
for(int p=1;p<=i3;p++)
{
byte[] b1=new byte[0];
string file2="";
this.NameExisted(1,sp_file1,out file2);
if(p!=i3)
{
b1=new byte[m/i3];
file1_fs.Read(b1,0,m/i3);
FileStream file2_fs =new FileStream(file2,FileMode.Create);
file2_fs.Write(b1,0,m/i3);
file2_fs.Close();
}
else //最后一个部分的读取
{
b1 =new byte[m-(p-1)*m/i3];
file1_fs.Read(b1,0,m-(p-1)*m/i3);
FileStream file2_fs =new FileStream(file2,FileMode.Create);
file2_fs.Write(b1,0,m-(p-1)*m/i3);
file2_fs.Close();
}
// file1_fs.Seek(0,SeekOrigin.Current); 可要可不要
}
file1_fs.Close();
}
break;
case("merge"): //合并
string[] mer = this.name1.Value.Split(new char []{'*'});
bool dy=true; //是否执行合并的参数
foreach(string mer_file in mer)
{
if(mer_file!=""&&Directory.Exists(ServerDir+mer_file)) //是否有文件夹
{
dy=false;
}
}
if(mer.Length==1) //没有选中任何文件
{
dy=false;
}
if(dy)
{
string mer_name1="";
this.NameExisted(0,ServerDir+mer[0],out mer_name1);
File.Copy(ServerDir+mer[0],mer_name1,true); // 创建一个文件副本
int t1 =mer[0].LastIndexOf("\\");
string mer_name2=mer[0].Substring(t1,mer[0].Length-t1);
File.Move(ServerDir+mer[0],ServerDir+"personal\\Recycler"+mer_name2);
//追加至此副本的文件尾
FileStream mer_fs1 =new FileStream(mer_name1,FileMode.Append);
for(int mi =1;mi<mer.Length;mi++)
{
if(mer[mi]!="")
{
FileStream mer_fs2 =new FileStream(ServerDir+mer[mi],FileMode.Open);
byte[] b2 = new byte[mer_fs2.Length];
int j=(int)mer_fs2.Length;
mer_fs2.Read(b2,0,j);
mer_fs2.Close();
int tm =mer[mi].LastIndexOf("\\");
string mer_name3=mer[mi].Substring(tm,mer[mi].Length-tm);
//并入副本后移至 personal\Recycler 文件夹
File.Move(ServerDir+mer[mi],ServerDir+"personal\\Recycler"+mer_name3);
mer_fs1.Write(b2,0,j);
}
}
mer_fs1.Close();
}
break;
case("copy"): //复制
StreamWriter copy_sw = File.CreateText(ServerDir+"action.dat");
copy_sw.Write("copy*");
string [] copy_tmp =this.name1.Value.Split(new char[]{'*'});
foreach(string copy_tmp1 in copy_tmp)
{
if(copy_tmp1!="")
{
copy_sw.Write(copy_tmp1+"*"); //记录所复制的文件列表
}
}
copy_sw.Close();
break;
case("cut"): //剪切
StreamWriter cut_sw = File.CreateText(ServerDir+"action.dat");
cut_sw.Write("cut*");
string [] cut_tmp =this.name1.Value.Split(new char[]{'*'});
foreach(string cut_tmp1 in cut_tmp)
{
if(cut_tmp1!="")
{
cut_sw.Write(cut_tmp1+"*"); //记录所剪切的文件列表
}
}
cut_sw.Close();
break;
case("paste"): //粘贴
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -