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

📄 filemanager.cs

📁 火车票的销售系统
💻 CS
字号:
using System;

using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
namespace filemanger
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.TextBox txtCurPath;
		private System.Windows.Forms.ListView FileView;
		private System.Windows.Forms.MenuItem newFolder;
		private System.Windows.Forms.MenuItem newFile;
		private System.Windows.Forms.MenuItem delete;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.txtCurPath = new System.Windows.Forms.TextBox();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.newFolder = new System.Windows.Forms.MenuItem();
			this.newFile = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.delete = new System.Windows.Forms.MenuItem();
			this.FileView = new System.Windows.Forms.ListView();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(0, 32);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(56, 24);
			this.label1.TabIndex = 0;
			this.label1.Text = "当前路径";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(280, 32);
			this.button1.Name = "button1";
			this.button1.TabIndex = 1;
			this.button1.Text = "返回上级目录";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// txtCurPath
			// 
			this.txtCurPath.Location = new System.Drawing.Point(64, 32);
			this.txtCurPath.Name = "txtCurPath";
			this.txtCurPath.Size = new System.Drawing.Size(208, 21);
			this.txtCurPath.TabIndex = 2;
			this.txtCurPath.Text = "";
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem1,
																					  this.menuItem6});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem2,
																					  this.menuItem5});
			this.menuItem1.Text = "文件";
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 0;
			this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.newFolder,
																					  this.newFile});
			this.menuItem2.Text = "新建";
			// 
			// newFolder
			// 
			this.newFolder.Index = 0;
			this.newFolder.Text = "文件夹";
			this.newFolder.Click += new System.EventHandler(this.newFolder_Click);
			// 
			// newFile
			// 
			this.newFile.Index = 1;
			this.newFile.Text = "文件";
			this.newFile.Click += new System.EventHandler(this.newFile_Click);
			// 
			// menuItem5
			// 
			this.menuItem5.Index = 1;
			this.menuItem5.Text = "退出";
			this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 1;
			this.menuItem6.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.delete});
			this.menuItem6.Text = "编辑";
			// 
			// delete
			// 
			this.delete.Index = 0;
			this.delete.Text = "删除";
			this.delete.Click += new System.EventHandler(this.delete_Click);
			// 
			// FileView
			// 
			this.FileView.Location = new System.Drawing.Point(0, 72);
			this.FileView.Name = "FileView";
			this.FileView.Size = new System.Drawing.Size(368, 192);
			this.FileView.TabIndex = 4;
			this.FileView.DoubleClick += new System.EventHandler(this.FileView_DoubleClick_1);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(376, 266);
			this.Controls.Add(this.FileView);
			this.Controls.Add(this.txtCurPath);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.label1);
			this.Menu = this.mainMenu1;
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
			
           
		}
        StringCollection CurPath= new StringCollection();
		private void Form1_Load(object sender, System.EventArgs e)
		{
		
			try
			{
				txtCurPath.Text = "我的电脑";
				FileView.Clear();
				FileView.View = View.Details;
				FileView.Columns.Add("本地磁盘", FileView.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];
					FileView.Items.Add(lvi);
				}

			}
			catch (Exception e1)
			{
                
				MessageBox.Show(e1.Message);
			}
		}
        
		void FillFilesView(string FullName)
		{
			FileView.Clear();
			FileView.View = View.Details;
			FileView.Columns.Add("名称",FileView.Width/3, HorizontalAlignment.Left);
			FileView.Columns.Add("类型", FileView.Width / 6, HorizontalAlignment.Center);
			FileView.Columns.Add("大小",FileView.Width/6, HorizontalAlignment.Center);
			FileView.Columns.Add("最后访问时间", FileView.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());
				FileView.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());
				FileView.Items.Add(lvi);
			}
            
		}
		

		private void button1_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);
					txtCurPath.Text = "我的电脑";
					FileView.Clear();
					FileView.View = View.Details;
					FileView.Columns.Add("本地磁盘", FileView.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];
						FileView.Items.Add(lvi);
					}
				}
			}
			catch (Exception e1)
			{
                
				MessageBox.Show(e1.Message);
			}
		}

		private void newFolder_Click(object sender, System.EventArgs e)
		{
			this.Visible=false;
			try
			{
				NewFolder folderDlg=new NewFolder();
				folderDlg.lbParentPath.Text = CurPath[CurPath.Count - 1];
				if(folderDlg.ShowDialog()==DialogResult.OK)
				{
					string CurFullPath = CurPath[CurPath.Count - 1];
					FillFilesView(CurFullPath);
				}
				folderDlg.Dispose();
				this.Visible=true;
			}
			catch (Exception e2)
			{
                
				MessageBox.Show(e2.Message);
			}
		}

		private void newFile_Click(object sender, System.EventArgs e)
		{
			this.Visible=false;
			try
			{
				NewFile fileDlg=new NewFile();
				fileDlg.lbParentPath.Text = CurPath[CurPath.Count - 1];
				if(fileDlg.ShowDialog()==DialogResult.OK)
				{
					string CurFullPath = CurPath[CurPath.Count - 1];
					FillFilesView(CurFullPath);
				}
				fileDlg.Dispose();
				this.Visible=true;
			}
			catch (Exception e3)
			{
                
				MessageBox.Show(e3.Message);
			}
		}

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

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

		private void FileView_DoubleClick_1(object sender, System.EventArgs e)
		{
			try
			{
				string FullName = FileView.SelectedItems[0].Tag.ToString();
				if (FileView.SelectedItems[0].ImageIndex == 1)
				{
					this.Visible=false;
					EditText dlgEditTxt = new EditText();
					dlgEditTxt.lbFullName.Text = FullName; 
					dlgEditTxt.ShowDialog(this);
					this.Visible=true;
				}
				else
					if (FileView.SelectedItems[0].ImageIndex == 2)
					System.Diagnostics.Process.Start(FullName);
				else
				{
					txtCurPath.Text = FullName;
					FillFilesView(FullName);
					CurPath.Add(FullName);
				}
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		

		

		
	}
}

⌨️ 快捷键说明

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