⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webform1.aspx.cs

📁 ASP.Net网络资源管理器及源代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
					string [] paste_tmp =this.name1.Value.Split(new char[]{'*'});
					if(paste_tmp[0]=="")
					{
						paste_tmp[0]="personal";   //没有选中任何文件夹,则默认为主目录
					}			
					StreamReader paste_sw = new StreamReader(ServerDir+"action.dat");
					string paste_tmp1=paste_sw.ReadToEnd();
					paste_sw.Close();
					string[] paste_tmp2=paste_tmp1.Split(new char []{'*'});
					if(paste_tmp2[0]=="copy")      //判断上一次操作
					{
						string paste_name="";
						for(int p=1;p< paste_tmp2.Length;p++)
						{
							if(paste_tmp2[p]!="")
							{
								//获取文件名

								int pa =paste_tmp2[p].LastIndexOf("\\");
								string paste_tmp3 =paste_tmp2[p].Substring(pa,paste_tmp2[p].Length-pa);

								//执行相应操作

								if(Directory.Exists(ServerDir+paste_tmp2[p]))
								{
									this.NameExisted(1,ServerDir+paste_tmp[0]+paste_tmp3,out paste_name);
									this.CopyDir(ServerDir+paste_tmp2[p],paste_name);
								}
								else if(File.Exists(ServerDir+paste_tmp2[p]))
								{
									this.NameExisted(1,ServerDir+paste_tmp[0]+paste_tmp3,out paste_name);
									File.Copy(ServerDir+paste_tmp2[p],paste_name,true);
								}
							}
						}
					}
					else if(paste_tmp2[0]=="cut")
					{
						string paste_name1="";
						for(int t=1;t< paste_tmp2.Length;t++)
						{
							if(paste_tmp2[t]!="")
							{
								int y =paste_tmp2[t].LastIndexOf("\\");
								string paste_tmp4 =paste_tmp2[t].Substring(y,paste_tmp2[t].Length-y);
								if(Directory.Exists(ServerDir+paste_tmp2[t]))
								{
									this.NameExisted(1,ServerDir+paste_tmp[0]+paste_tmp4,out paste_name1);
									Directory.Move(ServerDir+paste_tmp2[t],paste_name1);
								}
								else if(File.Exists(ServerDir+paste_tmp2[t]))
								{
									this.NameExisted(1,ServerDir+paste_tmp[0]+paste_tmp4,out paste_name1);
									File.Move(ServerDir+paste_tmp2[t],paste_name1);
								}
							}
						}
					}
					this.AddDirState(paste_tmp[0]);

					//最后清空action.dat

					StreamWriter paste_sw1 = File.CreateText(ServerDir+"action.dat");  
					paste_sw1.Write("");
					paste_sw1.Close();
					break;
			}
			this.BrowseDir(ServerDir+"personal",1,false);
			this.ClearDirState();

			// 在此处放置用户代码以初始化页面
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		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+="&nbsp;&nbsp;&nbsp;&nbsp;";
			}
			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()+"&nbsp; KB";
				}
				else
				{
					size = ii.ToString()+"&nbsp; 字节";
				}

				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\">&nbsp;&nbsp;<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\">&nbsp;&nbsp;<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()+"&nbsp; KB";
				}
				else
				{
					fi_tmp = fi.Length.ToString()+"&nbsp; 字节";
				}

				//添加文件信息

				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\">&nbsp;&nbsp;<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);
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -