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

📄 filemanager.cs

📁 需要的下
💻 CS
📖 第 1 页 / 共 2 页
字号:
		#endregion

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

		private void FillFilesView(string FullName)
		{
			FilesView.Clear();
			FilesView.View = View.Details;
			FilesView.Columns.Add("名称",FilesView.Width/3,HorizontalAlignment.Left);
			FilesView.Columns.Add("类型",FilesView.Width/6,HorizontalAlignment.Center);
			FilesView.Columns.Add("大小",FilesView.Width/6,HorizontalAlignment.Right);
			FilesView.Columns.Add("最后访问时间",FilesView.Width/3,HorizontalAlignment.Left);
			DirectoryInfo CurDir = new DirectoryInfo(FullName);
			DirectoryInfo[] dirs = CurDir.GetDirectories();
			FileInfo[] files = CurDir.GetFiles();
			//显示当前目录下的文件夹
			foreach(DirectoryInfo dir in dirs)
			{
				ListViewItem lvi = new ListViewItem();
				lvi.Text = dir.Name;
				lvi.ImageIndex = 0;
				lvi.Tag = dir.FullName;
				lvi.SubItems.Add("文件夹");
				lvi.SubItems.Add("");
				lvi.SubItems.Add(dir.LastAccessTime.ToString());
				FilesView.Items.Add(lvi);
			}
			//显示当前目录下的文件
			foreach(FileInfo file in files)
			{
				ListViewItem lvi = new ListViewItem();
				lvi.Text = file.Name;
				if(file.Extension == ".txt")
				{
					lvi.ImageIndex = 1;
				}
				else
				{
					lvi.ImageIndex = 2;
				}
				lvi.Tag = file.FullName;
				lvi.SubItems.Add("文件");
				lvi.SubItems.Add(file.Length.ToString());
				lvi.SubItems.Add(file.LastAccessTime.ToString());
				FilesView.Items.Add(lvi);
			}
		}

		private void FilesView_DoubleClick(object sender, System.EventArgs e)
		{
			try
			{
				//获得所选条目的全名称
				string FullName = FilesView.SelectedItems[0].Tag.ToString();
				if(FilesView.SelectedItems[0].ImageIndex == 1)
				{
					//若双击txt文件,则创建一个EditTxt对话框进行编辑
					EditTxt dlgEditTxt = new EditTxt();
					dlgEditTxt.lbFullName = FullName;
					dlgEditTxt.ShowDialog(this);
				}
				else	//若双击其他文件,则调用系统函数运行此文件
					if(FilesView.SelectedItems[0].ImageIndex == 2)
					System.Diagnostics.Process.Start(FullName);
				else
				{
					//若双击文件夹,则进入子目录
					txtCurPath.Text = FullName;
					FillFilesView(FullName);
					CurPath.Add(FullName);
				}
			}
			catch(Exception exx)
			{
				MessageBox.Show(exx.Message);
			}
		}

		private void btnUp_Click(object sender, System.EventArgs e)
		{
			try
			{
				if(CurPath.Count > 1)
				{
					string FullName = CurPath[CurPath.Count - 2].ToString();
					txtCurPath.Text = FullName;
					FillFilesView(FullName);
					CurPath.RemoveAt(CurPath.Count - 1);
				}
				else
				{
					if(CurPath.Count == 1)
						CurPath.RemoveAt(CurPath.Count - 1);
					this.txtCurPath.Text = "My Computer";
					this.FilesView.Clear();
					this.FilesView.View = View.Details;
					this.FilesView.Columns.Add("本地磁盘",FilesView.Width/3,HorizontalAlignment.Left);
					string[] Drv = Directory.GetLogicalDrives();
					int DrvCnt = Drv.Length;
					for(int i = 0;i<DrvCnt;i++)
					{
						ListViewItem lvi = new ListViewItem();
						lvi.Text = "驱动器" + Drv[i];
						lvi.ImageIndex = 3;
						lvi.Tag = Drv[i];
						this.FilesView.Items.Add(lvi);
					}
				}
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void NewFolder_Click(object sender, System.EventArgs e)
		{
			try
			{
				NewFolder folderDlg = new NewFolder();
				folderDlg.LbParentPath = CurPath[CurPath.Count - 1];
				if(folderDlg.ShowDialog() == DialogResult.OK)
				{
					string CurFullPath = CurPath[CurPath.Count - 1];
					FillFilesView(CurFullPath);
				}
				folderDlg.Dispose();
			}
			catch(Exception exx)
			{
				MessageBox.Show(exx.Message);
			}
		}

		private void NewFile_Click(object sender, System.EventArgs e)
		{
			try
			{
				NewFile fileDlg = new NewFile();
				fileDlg.LbParentPath = CurPath[CurPath.Count - 1];
				if(fileDlg.ShowDialog() == DialogResult.OK)
				{
					string CurFullPath = CurPath[CurPath.Count - 1];
					FillFilesView(CurFullPath);
				}
				fileDlg.Dispose();
			}
			catch(Exception exx)
			{
				MessageBox.Show(exx.Message);
			}
		}

		private void Delete_Click(object sender, System.EventArgs e)
		{
			try
			{
				if(FilesView.SelectedItems.Count == 0)return;
				if(FilesView.SelectedItems[0].SubItems[1].Text == "文件夹")
				{
					string strDir = FilesView.SelectedItems[0].Tag.ToString();
					DialogResult dlgRslt = MessageBox.Show("确定删除文件夹" + strDir + "?","确定删除",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
					if(dlgRslt == DialogResult.OK)
					{
						Directory.Delete(strDir,false);
						string CurFullPath = CurPath[CurPath.Count -1];
						FillFilesView(CurFullPath);
						MessageBox.Show("文件夹:" + strDir + "已经成功删除!");
					}
				}
				else
				{
					string strFile = FilesView.SelectedItems[0].Tag.ToString();
					DialogResult dlgRslt = MessageBox.Show("确定删除文件" + strFile + "?","确定删除",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
					if(dlgRslt == DialogResult.OK)
					{
						File.Delete(strFile);
						string CurFullPath = CurPath[CurPath.Count -1];
						FillFilesView(CurFullPath);
						MessageBox.Show("文件:" + strFile + "已经成功删除!");
					}
				}
			}
			catch(Exception exx)
			{
				MessageBox.Show(exx.Message);
			}
		}

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

		private void ContextmenuItemDelete_Click(object sender, System.EventArgs e)
		{
			try
			{
				if(FilesView.SelectedItems.Count == 0)return;
				if(FilesView.SelectedItems[0].SubItems[1].Text == "文件夹")
				{
					string strDir = FilesView.SelectedItems[0].Tag.ToString();
					DialogResult dlgRslt = MessageBox.Show("确定删除文件夹" + strDir + "?","确定删除",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
					if(dlgRslt == DialogResult.OK)
					{
						Directory.Delete(strDir,false);
						string CurFullPath = CurPath[CurPath.Count -1];
						FillFilesView(CurFullPath);
						MessageBox.Show("文件夹:" + strDir + "已经成功删除!");
					}
				}
				else
				{
					string strFile = FilesView.SelectedItems[0].Tag.ToString();
					DialogResult dlgRslt = MessageBox.Show("确定删除文件" + strFile + "?","确定删除",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
					if(dlgRslt == DialogResult.OK)
					{
						File.Delete(strFile);
						string CurFullPath = CurPath[CurPath.Count -1];
						FillFilesView(CurFullPath);
						MessageBox.Show("文件:" + strFile + "已经成功删除!");
					}
				}
			}
			catch(Exception exx)
			{
				MessageBox.Show(exx.Message);
			}
		}

		private void ContextmenuItemFolder_Click(object sender, System.EventArgs e)
		{
			try
			{
				NewFolder folderDlg = new NewFolder();
				folderDlg.LbParentPath = CurPath[CurPath.Count - 1];
				if(folderDlg.ShowDialog() == DialogResult.OK)
				{
					string CurFullPath = CurPath[CurPath.Count - 1];
					FillFilesView(CurFullPath);
				}
				folderDlg.Dispose();
			}
			catch(Exception exx)
			{
				MessageBox.Show(exx.Message);
			}
		}

		private void ContextmenuItemFile_Click(object sender, System.EventArgs e)
		{
			try
			{
				NewFile fileDlg = new NewFile();
				fileDlg.LbParentPath = CurPath[CurPath.Count - 1];
				if(fileDlg.ShowDialog() == DialogResult.OK)
				{
					string CurFullPath = CurPath[CurPath.Count - 1];
					FillFilesView(CurFullPath);
				}
				fileDlg.Dispose();
			}
			catch(Exception exx)
			{
				MessageBox.Show(exx.Message);
			}
		}

		private void ContextmenuItemFresh_Click(object sender, System.EventArgs e)
		{
			string CurFullPath = CurPath[CurPath.Count - 1];
			FillFilesView(CurFullPath);
		}
	}
}

⌨️ 快捷键说明

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