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

📄 form1.cs

📁 虚拟文件系统,多级目录
💻 CS
📖 第 1 页 / 共 2 页
字号:
			this.Load += new System.EventHandler(this.Form1_Load);
			this.tabControl1.ResumeLayout(false);
			this.tabPage1.ResumeLayout(false);
			this.tabPage2.ResumeLayout(false);
			this.tabPage3.ResumeLayout(false);
			this.tabControl2.ResumeLayout(false);
			this.tabPage4.ResumeLayout(false);
			this.tabPage5.ResumeLayout(false);
			this.tabPage6.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
		
		}
		private void show_dirfile(int a)/////显示目录中的子目录和文件
		{
			string mm="目录:\n";
			for(int i=1;i<21;i++)
			{
				if(dir[a,i]!=-1)
				{
					mm=mm+"\t"+dirname[dir[a,i]];
				}
			}
			mm=mm+"\n\n"+"文件:\n";
			for(int i=21;i<41;i++)
			{
				if(dir[a,i]!=-1)
				{
					mm=mm+"\t"+fname[dir[a,i]];
				}
			}
			
			this.show_dir.Text=mm;
		}
		///////////保存到硬盘
		private void saveall()
		{
			FileStream fs;
			if (!File.Exists("disk.data"))
			{
				fs = new FileStream("disk.data", FileMode.CreateNew);//将信息保存到硬盘上
			}
			else
			{
				fs = new FileStream("disk.data", FileMode.Open, FileAccess.ReadWrite);

			}
			BinaryWriter w = new BinaryWriter(fs);
			for(int i=0;i<200;i++)//保存目录信息
			{
				w.Write((string)dirname[i]);
				for(int j=0;j<41;j++)
				{
					w.Write((int)dir[i,j]);
				}
			}
			for(int i=0;i<200;i++)//保存文件信息
			{
				w.Write((string)fname[i]);
				w.Write((string)users[i]);
				for(int j=0;j<51;j++)
				{
					w.Write((int)file[i,j]);
				}
			}
			for(int i=0;i<10000;i++)//保存块信息
			{
				w.Write((bool)bfup[i]);
				for(int j=0;j<1024;j++)
				{
					w.Write((char)block[i,j]);
				}
			}
			w.Close();
			fs.Close();

		}
		private void show_here()
		{
			string sss="";
			for(int i=0;i<nh;i++)
			{
				sss=sss+"\\"+dirname[here[i]];
			}
			this.snd.Text="当前目录:disk:"+sss;
		}
		private void deld(int d)//删子目录
		{
			int a=d;
			for(int i=1;i<21;i++)//删目录
			{
				if(dir[a,i]!=-1)
				{
					this.deld(dir[a,i]);//嵌套删除子目录
					dirname[dir[a,i]]="";
					dir[dir[a,i],0]=-1;
					dir[a,i]=-1;									
				}
			}
			for(int i=21;i<41;i++)//删子目录下的文件
			{
				if(dir[a,i]!=-1)
				{
					for(int j=1;j<51;j++)//清空块
					{
						if(file[dir[a,i],j]!=-1)
						{
							int bl=file[dir[a,i],j];
							for(int k=0;k<1024;k++)
							{
								block[bl,k]='#';
							}
							bfup[bl]=false;
							file[dir[a,i],j]=-1;//清空文件的块表
						}
					}
					file[dir[a,i],0]=-1;//清除上级目录
					fname[dir[a,i]]="";//清除文件名
					users[dir[a,i]]="";
					dir[a,i]=-1;//将文件地址从所在目录的文件表中清除
			//		this.show_dirfile(a);
			//		this.show.Text="";
				}
			}
		}

		private void format_Click(object sender, System.EventArgs e)
		{
			for(int i=0;i<200;i++)//格式化目录\文件
			{
				dirname[i]="";
				fname[i]="";
				users[i]="";
				for(int j=0;j<41;j++)
				{
					dir[i,j]=-1;
				}
				for(int j=0;j<51;j++)
				{
					file[i,j]=-1;
				}
			}
			for(int i=0;i<10000;i++)  // 格式化块
			{
				bfup[i]=false;
				for(int j=0;j<1024;j++)
				{
					block[i,j]='#';
				}
			}
			dirname[0]="disk";
			dir[0,0]=0;
		/*	dir[0,1]=5;
			dir[0,2]=8;
			dir[0,30]=5;
		dirname[5]="ok";
			dirname[8]="okkkkk";
			dir[5,0]=0;
			dir[5,1]=3;
			dir[5,24]=9;
			dirname[3]="fffok";
			fname[5]="pp";
			fname[9]="jjjj";
			*/
			this.saveall();




			

		}

		private void open_Click(object sender, System.EventArgs e)
		{
			FileStream fs = new FileStream("disk.data", FileMode.Open, FileAccess.Read);
			BinaryReader r = new BinaryReader(fs);
			for(int i=0;i<200;i++)//读取目录信息
			{
				dirname[i]=r.ReadString();
				for(int j=0;j<41;j++)
				{
					dir[i,j]=r.ReadInt32();
				}
			}
			for(int i=0;i<200;i++)//读取文件信息
			{
				fname[i]=r.ReadString();
				users[i]=r.ReadString();
				for(int j=0;j<51;j++)
				{
					file[i,j]=r.ReadInt32();
				}
			}
			for(int i=0;i<10000;i++)//读取块信息
			{
				bfup[i]=r.ReadBoolean();
				for(int j=0;j<1024;j++)
				{
					block[i,j]=r.ReadChar();
				}
			}
			r.Close();
			fs.Close();
			//////////显示根目录
			now=0;
			this.show_dirfile(now);
			this.snd.Text=this.snd.Text+"disk:";
		/*	for(int i=0;i<20;i++)
			{
				here[i]=300;
			}
		*/	
		}

		private void b_indir_Click(object sender, System.EventArgs e)//进入子目录
		{
			int a=now;
			for(int i=1;i<21;i++)
			{
				if(dir[now,i]!=-1)
				{					
					if(indir.Text==dirname[dir[now,i]])
					{
						this.show_dirfile(dir[now,i]);
						a=dir[now,i];
						here[nh]=a;
						nh++;
						this.show_here();			
					}
				//	now=a;
				}
            now=a;
				
			}
		
		}

		private void up_dir_Click(object sender, System.EventArgs e)//返回上级目录
		{
			int up=dir[now,0];
			now=up;
			this.show_dirfile(up);
			
				if(nh!=0)nh--;
				this.show_here();
		
			
		}

		private void b_adddir_Click(object sender, System.EventArgs e)
		{
			
			for(int i=1;i<21;i++)
			{
				if(dir[now,i]!=-1)
				{
					if(this.add_dir.Text==dirname[dir[now,i]])
					{
						MessageBox.Show("出现重名!");
						return ;
					}
				}
			}
    //       新建目录
			for(int i=1;i<200;i++)
			{
				if(dir[i,0]==-1)
				{
					for(int j=1;j<21;j++)
					{
						if(dir[now,j]==-1)
						{
							dir[now,j]=i;
							dirname[i]=this.add_dir.Text;
							dir[i,0]=now;
							this.show_dirfile(now);
							return;
						}
					}
					
				}
			}
		//	this.show_dirfile(now);
		}

		private void exit_Click(object sender, System.EventArgs e)
		{
			this.saveall();
			this.Close();
		}

		private void newf_Click(object sender, System.EventArgs e)
		{
			for(int i=21;i<41;i++)
			{
				if(dir[now,i]!=-1)
				{
					if(this.newfile.Text==fname[dir[now,i]])
					{
						MessageBox.Show("出现重名!");
						return ;
					}
				}
			}
			//       新建文件
			for(int i=1;i<200;i++)
			{
				if(file[i,0]==-1)
				{
					for(int j=21;j<41;j++)
					{
						if(dir[now,j]==-1)
						{
							dir[now,j]=i;
							fname[i]=this.newfile.Text;
							users[i]=user;
							file[i,0]=now;
							this.show_dirfile(now);
							return;
						}
					}
					
				}
			}
		
		}

		private void fopen_Click(object sender, System.EventArgs e)
		{
			this.show.Text=null;
			bool have=false;
			for(int i=21;i<41;i++)
			{
				if(dir[now,i]!=-1)
				{
					if(this.infile.Text==fname[dir[now,i]])
					{
						have=true;
						this.openfile=dir[now,i];
						for(int j=1;j<51;j++)
						{
							if(file[dir[now,i],j]!=-1)
							{
								int bl=file[dir[now,i],j];
								for(int k=0;k<1024;k++)
								{
									if(block[bl,k]=='#')break;
									this.show.Text+=block[bl,k].ToString();
								}
							}
						}
						this.nowfile.Text="打开的文件是:"+fname[openfile];
						return ;
					}
				}
			}
			if(have==false)MessageBox.Show("文件不存在!!");
		}

		private void save_Click(object sender, System.EventArgs e)
		{
			if(users[openfile]!=user){MessageBox.Show("您无权删除此文件!!");return;}
			for(int i=1;i<51;i++)//清空原块
			{
				if(file[openfile,i]!=-1)
				{
					int bl=file[openfile,i];
					for(int k=0;k<1024;k++)
					{
						block[bl,k]='#';
					}
					this.bfup[bl]=false;
					file[openfile,i]=-1;
				}
			}
			//将文件保存到块中
			int len=this.show.Text.Length;
			int a=len%1024;//是否需要非整块
			int b=len/1024;//文件需要的整块数
			
			
			for(int j=0;j<b;j++)
			{
				for(int k=0;k<10000;k++)
				{
					bool pp=false;
					if(bfup[k]==false)
					{
						//将文件放入空块
						char[] blo;
						blo=this.show.Text.ToCharArray(j*1024,1024);
						for(int t=0;t<1024;t++)
						{
							block[k,t]=blo[t];
						}
						bfup[k]=true;
						pp=true;
					}
					file[openfile,j+1]=k;
					if(pp==true)break;
				}
			}
			if(a!=0)
			{
				for(int k=0;k<10000;k++)
				{
					bool pp=false;
					if(bfup[k]==false)
					{
						char[] blo;
						blo=this.show.Text.ToCharArray(b*1024,a);
						for(int t=0;t<a;t++)
						{
							block[k,t]=blo[t];
						}
						bfup[k]=true;
						pp=true;
					}
					
					file[openfile,b+1]=k;
					if(pp==true)break;
					
				}
			}
			

		
		}

		private void delf_Click(object sender, System.EventArgs e)
		{
			bool have=false;
			for(int i=21;i<41;i++)
			{
				if(dir[now,i]!=-1)
				{
					if(this.delfile.Text==fname[dir[now,i]])
					{
						if(users[dir[now,i]]!=user){MessageBox.Show("您无权删除此文件!!");return;}
						have=true;
					//	this.openfile=dir[now,i];
						for(int j=1;j<51;j++)//清空块
						{
							if(file[dir[now,i],j]!=-1)
							{
								int bl=file[dir[now,i],j];
								for(int k=0;k<1024;k++)
								{
									block[bl,k]='#';
								}
								bfup[bl]=false;
								file[dir[now,i],j]=-1;//清空文件的块表
							}
						}
						file[dir[now,i],0]=-1;//清除上级目录
						fname[dir[now,i]]="";//清除文件名
						users[dir[now,i]]="";
                        dir[now,i]=-1;//将文件地址从所在目录的文件表中清除
						this.show_dirfile(now);
					    this.show.Text="";
						return ;
					}
				}
			}
			if(have==false)MessageBox.Show("文件不存在!!");
		
		}

		private void b_deldir_Click(object sender, System.EventArgs e)
		{
			int a=now;
			bool have=false;
			for(int i=1;i<21;i++)
			{
				if(dir[a,i]!=-1)
				{					
					if(this.del_dir.Text==dirname[dir[a,i]])
					{
						have=true;
						this.deld(dir[a,i]);//删子目录
                        dirname[dir[a,i]]="";
						dir[dir[a,i],0]=-1;
						dir[a,i]=-1;
						this.show_dirfile(now);
				    }
					//	now=a;
				}
		//		now=a;
				
			}
			if(have==false)MessageBox.Show("目录不存在!!");
		}

		private void pass_Click(object sender, System.EventArgs e)
		{
			user=this.id.Text;
		}

		
	}
}

⌨️ 快捷键说明

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