今天继续C#的学习,今天的目标是完成一个简易的文件管理装置 具体要求如下
1.编写一个简易的文件管理器,通过本次实验,练习 TreeView、ListView 和
SplitContainer 控件的使用,同时熟悉 C#文件系统的操作方法以及 File 类和 Directory类的使用。
界面设计: partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.splitContainer1 = new System.Windows.Forms.SplitContainer(); this.treeView1 = new System.Windows.Forms.TreeView(); this.listView1 = new System.Windows.Forms.ListView(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); this.splitContainer1.Panel2.SuspendLayout(); this.splitContainer1.SuspendLayout(); this.SuspendLayout(); // // splitContainer1 // this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill; this.splitContainer1.Location = new System.Drawing.Point(0, 0); this.splitContainer1.Name = "splitContainer1"; // // splitContainer1.Panel2 // this.splitContainer1.Panel2.Controls.Add(this.listView1); this.splitContainer1.Panel2.Controls.Add(this.treeView1); this.splitContainer1.Size = new System.Drawing.Size(1255, 650); this.splitContainer1.SplitterDistance = 418; this.splitContainer1.TabIndex = 0; // // treeView1 // this.treeView1.Dock = System.Windows.Forms.DockStyle.Left; this.treeView1.Location = new System.Drawing.Point(0, 0); this.treeView1.Name = "treeView1"; this.treeView1.Size = new System.Drawing.Size(182, 650); this.treeView1.TabIndex = 0; // // listView1 // this.listView1.Dock = System.Windows.Forms.DockStyle.Fill; this.listView1.Location = new System.Drawing.Point(182, 0); this.listView1.MultiSelect = false; this.listView1.Name = "listView1"; this.listView1.Size = new System.Drawing.Size(651, 650); this.listView1.TabIndex = 1; this.listView1.UseCompatibleStateImageBehavior = false; this.listView1.View = System.Windows.Forms.View.List; this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged); // // Form1 // this.ClientSize = new System.Drawing.Size(1255, 650); this.Controls.Add(this.splitContainer1); this.Name = "Form1"; this.Text = "简易的文件管理器"; this.splitContainer1.Panel2.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit(); this.splitContainer1.ResumeLayout(false); this.ResumeLayout(false); } #endregion private SplitContainer splitContainer1; private ListView listView1; private TreeView treeView1; }
程序代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); TreeNode myComputerNode = new TreeNode("我的电脑"); treeView1.Nodes.Add(myComputerNode); listViewShow(myComputerNode); } private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { listViewShow(e.Node); DirTreeShow(e.Node); } private void listView1_SelectedIndexChanged(object sender, EventArgs e) { foreach (int listIndex in listView1.SelectedIndices) { listViewShow(listView1.Items[listIndex].Text); } } private void listViewShow(TreeNode dirNode) { listView1.Clear(); if (dirNode.Parent == null) { foreach (string DrvName in Directory.GetLogicalDrives()) { ListViewItem aItem = new ListViewItem(DrvName); listView1.Items.Add(aItem); } } else { foreach (string DirName in Directory.GetDirectories((string)dirNode.Tag)) { ListViewItem aItem = new ListViewItem(DirName); listView1.Items.Add(aItem); } foreach (string fileName in Directory.GetFiles((string)dirNode.Tag)) { ListViewItem aItem = new ListViewItem(fileName); listView1.Items.Add(aItem); } } } private void listViewShow(string dirName) { listView1.Clear(); try { foreach (string DirName in Directory.GetDirectories(dirName)) { ListViewItem aItem = new ListViewItem(DirName); listView1.Items.Add(aItem); } foreach (string fileName in Directory.GetFiles(dirName)) { ListViewItem aItem = new ListViewItem(fileName); listView1.Items.Add(aItem); } } catch { } } private void DirTreeShow(TreeNode dirNode) { try { if (dirNode.Nodes.Count == 0) { if (dirNode.Parent == null) { foreach (string DrvName in Directory.GetLogicalDrives()) { TreeNode aNode = new TreeNode(DrvName); aNode.Tag = DrvName; dirNode.Nodes.Add(aNode); } } else { foreach (string DirName in Directory.GetDirectories((string)dirNode.Tag)) { TreeNode aNode = new TreeNode(DirName); aNode.Tag = DirName; dirNode.Nodes.Add(aNode); } } } } catch { } } }
标签:listView1,string,treeView1,C#,splitContainer1,System,学习,new From: https://www.cnblogs.com/1774323810com/p/16999313.html