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

📄 form1.cs

📁 Csharp实例编程百例.rar
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace FileManagerApp
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TreeView DirTree;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.ListView FileList;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.MenuItem menuItem7;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

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

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
			TreeNode MyComputerNode = new TreeNode ("我的电脑");
			DirTree.Nodes.Add (MyComputerNode);
			FileListShow(MyComputerNode);	//初始化ListView控件
		}

		private void FileListShow(TreeNode aDirNode)
		{
			FileList.Clear ();

			try
			{
				if (aDirNode.Parent == null)
				{				
					foreach (string DrvName in Directory.GetLogicalDrives ())
					{
						ListViewItem aItem = new ListViewItem(DrvName);
						FileList.Items.Add (aItem);
					}
				}
				else
				{			
					foreach(string DirName in Directory.GetDirectories ((string)aDirNode.Tag ))
					{
						ListViewItem aItem = new ListViewItem(DirName);
						FileList.Items.Add (aItem);
					}
					foreach(string FileName in Directory.GetFiles  ((string)aDirNode.Tag ))
					{
						ListViewItem aItem = new ListViewItem(FileName);
						FileList.Items.Add (aItem);
					}				
				}
			}
			catch{}
		}
		private void FileListShow(string aDirName)
		{
			FileList.Clear ();
			try
			{
				foreach(string DirName in Directory.GetDirectories (aDirName ))
				{
					ListViewItem aItem = new ListViewItem(DirName);
					FileList.Items.Add (aItem);
				}
				foreach(string FileName in Directory.GetFiles  (aDirName))
				{
					ListViewItem aItem = new ListViewItem(FileName);
					FileList.Items.Add (aItem);
				}
			}
			catch{}
		}

		private void DirTreeShow( TreeNode aDirNode )
		{
			try
			{
				if (aDirNode.Nodes.Count == 0)
				{
					if (aDirNode.Parent == null)
					{				
						foreach (string DrvName in Directory.GetLogicalDrives ())
						{
							TreeNode aNode = new TreeNode(DrvName);
							aNode.Tag = DrvName;
							aDirNode.Nodes.Add (aNode);
						}			
					}
					else 
					{
						foreach (string DirName in Directory.GetDirectories((string)aDirNode.Tag ))
						{
							TreeNode aNode = new TreeNode (DirName);
							aNode.Tag = DirName;
							aDirNode.Nodes.Add (aNode);
						}
					}
				}
			}
			catch{}
		}
		
		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.DirTree = new System.Windows.Forms.TreeView();
			this.splitter1 = new System.Windows.Forms.Splitter();
			this.FileList = new System.Windows.Forms.ListView();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.menuItem7 = new System.Windows.Forms.MenuItem();
			this.SuspendLayout();
			// 
			// DirTree
			// 
			this.DirTree.Dock = System.Windows.Forms.DockStyle.Left;
			this.DirTree.ImageIndex = -1;
			this.DirTree.Name = "DirTree";
			this.DirTree.SelectedImageIndex = -1;
			this.DirTree.Size = new System.Drawing.Size(121, 273);
			this.DirTree.TabIndex = 0;
			this.DirTree.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.DirTree_AfterSelect);
			// 
			// splitter1
			// 
			this.splitter1.Location = new System.Drawing.Point(121, 0);
			this.splitter1.Name = "splitter1";
			this.splitter1.Size = new System.Drawing.Size(3, 273);
			this.splitter1.TabIndex = 1;
			this.splitter1.TabStop = false;
			// 
			// FileList
			// 
			this.FileList.Dock = System.Windows.Forms.DockStyle.Fill;
			this.FileList.Location = new System.Drawing.Point(124, 0);
			this.FileList.MultiSelect = false;
			this.FileList.Name = "FileList";
			this.FileList.Size = new System.Drawing.Size(168, 273);
			this.FileList.TabIndex = 2;
			this.FileList.View = System.Windows.Forms.View.List;
			this.FileList.DoubleClick += new System.EventHandler(this.FileList_DoubleClick);
			this.FileList.SelectedIndexChanged += new System.EventHandler(this.FileList_SelectedIndexChanged);
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem1,
																					  this.menuItem2,
																					  this.menuItem7});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem6});
			this.menuItem1.Text = "文件(&F)";
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 0;
			this.menuItem6.Text = "Exit";
			this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 1;
			this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem3,
																					  this.menuItem4,
																					  this.menuItem5});
			this.menuItem2.Text = "编辑(&E)";
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 0;
			this.menuItem3.Shortcut = System.Windows.Forms.Shortcut.CtrlC;
			this.menuItem3.Text = "复制";
			this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 1;
			this.menuItem4.Shortcut = System.Windows.Forms.Shortcut.CtrlV;
			this.menuItem4.Text = "粘贴";
			this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
			// 
			// menuItem5
			// 
			this.menuItem5.Index = 2;
			this.menuItem5.Shortcut = System.Windows.Forms.Shortcut.CtrlX;
			this.menuItem5.Text = "剪切";
			// 
			// menuItem7
			// 
			this.menuItem7.Index = 2;
			this.menuItem7.Text = "关于(&A)";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(292, 273);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.FileList,
																		  this.splitter1,
																		  this.DirTree});
			this.Menu = this.mainMenu1;
			this.Name = "Form1";
			this.Text = "FileManagerApp";
			this.ResumeLayout(false);

		}
		#endregion

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

		private void DirTree_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			FileListShow( e.Node );
			DirTreeShow( e.Node );
		}

		private void FileList_DoubleClick(object sender, System.EventArgs e)
		{
			foreach (int ListIndex in FileList.SelectedIndices )
			{
				FileListShow( FileList.Items[ListIndex].Text );
			}								
		}

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

		private string SourceFileName;
		
		private void menuItem3_Click(object sender, System.EventArgs e)
		{			
			if (DirTree.Focused )
			{
				SourceFileName =  DirTree.SelectedNode .Text ;			
			}
			else if (FileList.Focused )
			{
				foreach (int ListIndex in FileList.SelectedIndices )
				{
					SourceFileName = FileList.Items[ListIndex].Text ;					
				}								
			}
			MessageBox.Show ("Copy File"+SourceFileName);	
		}

		private void menuItem4_Click(object sender, System.EventArgs e)
		{
			string CurPath = "";

			if (DirTree.Focused )
			{
				CurPath = DirTree.SelectedNode .Text ;
			}
			else if(FileList.Focused )
			{
				CurPath = Directory.GetParent (FileList.Items[0].Text ).FullName ;
			}
			try
			{
				MessageBox.Show ("Copy to "+ CurPath);
				File.Copy (SourceFileName,CurPath,true);			
			}
			catch{}				
		}
				
		private void FileList_SelectedIndexChanged(object sender, System.EventArgs e)
		{
		
		}
	}
}

⌨️ 快捷键说明

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