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; class fileop ... { public bool DirState(string d) ...{ //判断目录打开的状态 string ServerDir = MapPath(".")+"/"; if(!File.Exists(ServerDir+"DirState.dat")) ...{ StreamWriter sw =File.CreateText(ServerDir+"DirState.dat"); sw.Write(""); sw.Close(); } StreamReader sr=new StreamReader(ServerDir+"DirState.dat"); string tmp = sr.ReadToEnd(); sr.Close(); string[] s =tmp.Split(new char []...{'*'}); bool bl=false; foreach(string s1 in s) ...{ if(s1==d) ...{ bl= true; } } return bl; } public string Space(int b) ...{ //添加空格 string c =""; for(int m =1;m<=b;m++) ...{ c+=" "; } return c; } public string ShortDir(string s) ...{ //将文件的绝对路径转为相对路径 string ServerDir = MapPath(".")+"/"; string d=s.Substring(ServerDir.Length,s.Length-ServerDir.Length); return d; } public string OtherName(string s) ...{ //获取特殊名称,仅为 javascript:Go("...",".../..."); string ServerDir = MapPath(".")+"/"; string tmp = s.Remove(0,ServerDir.Length); string [] name =tmp.Split(new char []...{'/'}); string d=""; for(int i=0;i<name.Length;i++) ...{ if(i!=0) ...{ d+="/"+"/"+name[i]; } else ...{ d+=name[i]; } } return d; } public void BrowseDir(string s,int i,bool b) ...{ //浏览目录 DirectoryInfo dir =new DirectoryInfo(s); foreach(DirectoryInfo di in dir.GetDirectories()) //添加目录信息 ...{ long ii = this.DirSize(di.FullName); string size=""; if(ii>=1024) ...{ size = (ii/1024).ToString()+" KB"; } else ...{ size = ii.ToString()+" 字节"; } if(b||this.Expand(this.ShortDir(di.FullName))||this.DirState(this.ShortDir(di.FullName))) //此目录为“打开”状态 ...{ this.AddDirState(this.ShortDir(di.FullName)); this.td1.Controls.Add(new LiteralControl(this.Space(i)+"<input type="checkbox" name=""+this.ShortDir(di.FullName))); this.td1.Controls.Add(new LiteralControl( ""value="dir"><IMG alt="" src="OPENFOLD.BMP"> <a href="javascript: Go('open','"+this.OtherName(di.FullName)+"');">")); this.td1.Controls.Add(new LiteralControl(di.Name+"</a>"+this.Space(2)+"<font size=2>"+size+this.Space(1))); this.td1.Controls.Add(new LiteralControl(di.LastWriteTime.ToShortDateString()+" "+di.LastWriteTime.ToShortTimeString()+"</font><br>")); if(b||this.Expand(this.ShortDir(di.FullName))) //此目录为“展开” ...{ this.BrowseDir(di.FullName,i+2,true); //继续展开子目录 } else //此目录为“打开” ...{ this.BrowseDir(di.FullName,i+2,false); //显示此目录所有的文件及子目录 } } else //此目录为“关闭”状态 ...{ this.td1.Controls.Add(new LiteralControl(this.Space(i)+"<input type="checkbox" name=""+this.ShortDir(di.FullName))); this.td1.Controls.Add(new LiteralControl( ""value="dir"><IMG alt="" src="CLSDFOLD.BMP"> <a href="javascript: Go('open','"+this.OtherName(di.FullName)+"');">")); this.td1.Controls.Add(new LiteralControl(di.Name+"</a>"+this.Space(2)+"<font size=2>"+size+this.Space(1))); this.td1.Controls.Add(new LiteralControl(di.LastWriteTime.ToShortDateString()+" "+di.LastWriteTime.ToShortTimeString()+"</font><br>")); } } foreach(FileInfo fi in dir.GetFiles()) ...{ string fi_tmp=""; if(fi.Length>=1024) ...{ fi_tmp = (fi.Length/1024).ToString()+" KB"; } else ...{ fi_tmp = fi.Length.ToString()+" 字节"; } //添加文件信息 this.td1.Controls.Add(new LiteralControl(this.Space(i)+"<input type="checkbox" name=""+this.ShortDir(fi.FullName))); this.td1.Controls.Add(new LiteralControl( ""value="file"><IMG alt="" src="TXT.BMP"> <a href="javascript: Go('open','"+this.OtherName(fi.FullName)+"');">")); this.td1.Controls.Add(new LiteralControl(fi.Name+"</a>"+this.Space(2)+"<font size=2>"+fi_tmp+this.Space(1))); this.td1.Controls.Add(new LiteralControl(fi.LastWriteTime.ToShortDateString()+" "+fi.LastWriteTime.ToShortTimeString()+"</font><br>")); } } public bool Expand(string s) ...{ //判断此目录是否需要展开 string[] tmp=this.Expand_Dir.Split(new char []...{'*'}); bool b =false; foreach(string dd in tmp) ...{ if(dd==s) ...{ b=true; } } return b; } public void NameExisted(int i,string s, out string ss) ...{ //如果文件名称已存在,则重新命名 string[] tmp =s.Split(new char []...{'/'}); string name=tmp[tmp.Length-1]; //从文件的绝对路径名分离出文件名 int l=s.LastIndexOf("/")+1; A: int j=name.LastIndexOf(")")-name.LastIndexOf("("); if(Directory.Exists(s)) //文件夹的重新命名 ...{ i++; if(j>1&&name.Length==name.LastIndexOf(")")+1) //判断是否以(i)结尾 ...{ name=name.Remove(name.LastIndexOf("("),j+1); } name+="("+i.ToString()+")"; s=s.Remove(l,s.Length-l); s+=name; goto A; } else if(File.Exists(s)) //重新命名文件 ...{ i++; int n =name.LastIndexOf("."); if(n<0) //是否有后缀名 ...{ if(j>1&&name.Length==name.LastIndexOf(")")+1) ...{ name=name.Remove(name.LastIndexOf("("),j+1); } name+="("+i.ToString()+")"; } else ...{ if(j>1 && n==name.LastIndexOf(")")+1) //后缀名是否紧挨着(i) ...{ name=name.Remove(name.LastIndexOf("("),j+1); } name=name.Insert(name.LastIndexOf("."),"("+i.ToString()+")"); } s=s.Remove(l,s.Length-l); s+=name; goto A; } ss=s; } public void ClearDirState() ...{ //清理 DirState.dat 文件 string ServerDir = MapPath(".")+"/"; StreamReader sr = new StreamReader(ServerDir+"DirState.dat"); string tmp = sr.ReadToEnd(); sr.Close(); string[] tmp2 = tmp.Split(new char []...{'*'}); foreach(string s in tmp2) ...{ if(s!="") ...{ if(!Directory.Exists(ServerDir+s)) ...{ tmp=tmp.Replace(s+"*",""); } } } StreamWriter sw =File.CreateText(ServerDir+"DirState.dat"); sw.Write(tmp); sw.Close(); } public void AddDirState(string s) ...{ //向 DirState.dat 添加目录 string ServerDir = MapPath(".")+"/"; StreamReader sr=new StreamReader(ServerDir+"DirState.dat"); string tmp = sr.ReadToEnd(); sr.Close(); if(tmp.IndexOf(s+"*")<0) ...{ StreamWriter sw =File.AppendText(ServerDir+"DirState.dat"); sw.Write(s+"*"); sw.Close(); } } public long DirSize(string s) ...{ //获取目录的大小 long i=0L; DirectoryInfo dir = new DirectoryInfo(s); foreach (FileInfo f in dir.GetFiles()) ...{ i+=f.Length; } foreach(DirectoryInfo subdir in dir.GetDirectories()) ...{ i+=this.DirSize(subdir.FullName); } return i; } public bool CheckName(string s) ...{ //检查文件名是否合法 bool b=true; if(s.IndexOfAny(new char[]...{'?','/','/','<','>',':','*','"','|'})>=0||s=="") ...{ b=false; } return b; } public void CopyDir(string s, string d) ...{ //复制目录的方法 Directory.CreateDirectory(d); DirectoryInfo di =new DirectoryInfo(s); foreach(DirectoryInfo dir in di.GetDirectories()) ...{ this.CopyDir(dir.FullName,d+"/"+dir.Name); } foreach(FileInfo fi in di.GetFiles()) ...{ fi.CopyTo(d+"/"+fi.Name,true); } }}