C#学习5
今天继续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 { } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!