每日博客

C#

编写一个简易的文件管理器,通过本次实验,练习 TreeView、ListView和

SplitContainer 控件的使用,同时熟悉 C#文件系统的操作方法以及 File 类和 Directory类的使用。

(1)新建 windows 应用程序。

(2)将窗体 Form 的 Text 属性设置为“简易的文件管理器”。

(3)窗体上添加 SplitContainer、TreeView、ListView 控件,设置属性。将 TreeView 控

件的 Dock 属性设置为 Left,ListView 控件的 Dock 属性设置为 Fill,MultiSelect 属性设置为False,View 属性设置为 List。

(4)在 Form1.cs 中,添加对命名空间 using System.IO 的引用。

(5)在 Form1 的构造函数中添加代码:

public Form1()        

{            

InitializeComponent();            

TreeNodemyComputerNode = new TreeNode("我的电脑");             treeView1.Nodes.Add(myComputerNode);             listViewShow(myComputerNode);        

}

(6)编写 ListView 控件的方法 listViewShow(TreeNodedirNode)。

private void listViewShow(TreeNodedirNode)

{

listView1.Clear();

if (dirNode.Parent == null)

{

foreach (string DrvName in Directory.GetLogicalDrives())

{

ListViewItemaItem = new ListViewItem(DrvName);

    listView1.Items.Add(aItem);

}

}

else

{

foreach (string DirName in Directory.GetDirectories((string)dirNode.Tag))

{

ListViewItemaItem = new ListViewItem(DirName);

listView1.Items.Add(aItem);

}

foreach (string fileName in Directory.GetFiles((string) dirNode.Tag))

{

ListViewItemaItem=new ListViewItem(fileName);

listView1.Items.Add(aItem);

}

}

}

(7)编写 listViewShow 的重载方法。

private void listViewShow(string dirName)

{

listView1.Clear();

try

{

foreach (string DirName in Directory.GetDirectories(dirName))

{

ListViewItemaItem = new ListViewItem(DirName);

listView1.Items.Add(aItem);

}

foreach (string fileName in Directory.GetFiles(dirName))

{

ListViewItemaItem = new ListViewItem(fileName);

listView1.Items.Add(aItem);

}

}

catch

{

}

}

(8)编写 DirTreeShow(TreeNodedirNode)方法。

private void DirTreeShow(TreeNodedirNode)

{

try

{

if (dirNode.Nodes.Count == 0)                

{

if (dirNode.Parent == null)

{

foreach (string DrvName in Directory.GetLogicalDrives())

{

TreeNodeaNode= new TreeNode(DrvName);

aNode.Tag=DrvName;

dirNode.Nodes.Add(aNode);

}

}

else

{

foreach(string DirName in Directory.GetDirectories((string)dirNode.Tag))

{

TreeNodeaNode=new TreeNode(DirName);

aNode.Tag=DirName;

dirNode.Nodes.Add(aNode);

}

}

}

}

Catch

{

}

}

(9)编写 TreeView 控件的 AfterSelect 事件代码。

private void  treeView1_AfterSelect_1(object sender, TreeViewEventArgs e)

{

listViewShow(e.Node);

DirTreeShow(e.Node);

}

(10)编写 ListView 控件的 DoubleClick 事件代码。

private void  listView1_DoubleClick(object sender, EventArgs e)

{

foreach (int listIndex in listView1.SelectedIndices)

{

listViewShow(listView1.Items[listIndex].Text);

}

}

(11)单击启动调试工具,运行结果参见下图。

 

 

namespace WindowsFormsApplication1

{

    partial class Form1

    {

        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }

        #region Windows 窗体设计器生成的代码

        private void InitializeComponent()

        {

            this.components = new System.ComponentModel.Container();

            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));

            this.treeView1 = new System.Windows.Forms.TreeView();

            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);

            this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();

            this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();

            this.打开目录ToolStripMenuItem = new System.Windows.Forms.ToolStripSeparator();

            this.打开目录ToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();

            this.listView1 = new System.Windows.Forms.ListView();

            this.splitContainer1 = new System.Windows.Forms.SplitContainer();

            this.imageList1 = new System.Windows.Forms.ImageList(this.components);

            this.contextMenuStrip1.SuspendLayout();

            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();

            this.splitContainer1.Panel1.SuspendLayout();

            this.splitContainer1.Panel2.SuspendLayout();

            this.splitContainer1.SuspendLayout();

            this.SuspendLayout();

            this.treeView1.ContextMenuStrip = this.contextMenuStrip1;

            this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;

            this.treeView1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

            this.treeView1.Location = new System.Drawing.Point(0, 0);

            this.treeView1.Name = "treeView1";

            this.treeView1.Size = new System.Drawing.Size(237, 619);

            this.treeView1.TabIndex = 0;

            this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);

            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {

            this.toolStripMenuItem1,

            this.toolStripMenuItem2,

            this.打开目录ToolStripMenuItem,

            this.打开目录ToolStripMenuItem1});

            this.contextMenuStrip1.Name = "contextMenuStrip1";

            this.contextMenuStrip1.Size = new System.Drawing.Size(125, 76);

            this.toolStripMenuItem1.Name = "toolStripMenuItem1";

            this.toolStripMenuItem1.Size = new System.Drawing.Size(124, 22);

            this.toolStripMenuItem1.Text = "打开";

            this.toolStripMenuItem1.Click += new System.EventHandler(this.toolStripMenuItem1_Click);

            this.toolStripMenuItem2.Name = "toolStripMenuItem2";

            this.toolStripMenuItem2.Size = new System.Drawing.Size(124, 22);

            this.toolStripMenuItem2.Text = "删除";

            this.toolStripMenuItem2.Click += new System.EventHandler(this.toolStripMenuItem2_Click);

            this.打开目录ToolStripMenuItem.Name = "打开目录ToolStripMenuItem";

            this.打开目录ToolStripMenuItem.Size = new System.Drawing.Size(121, 6);

            this.打开目录ToolStripMenuItem1.Name = "打开目录ToolStripMenuItem1";

            this.打开目录ToolStripMenuItem1.Size = new System.Drawing.Size(124, 22);

            this.打开目录ToolStripMenuItem1.Text = "打开目录";

            this.打开目录ToolStripMenuItem1.Click += new System.EventHandler(this.打开目录ToolStripMenuItem1_Click);

            this.listView1.ContextMenuStrip = this.contextMenuStrip1;

            this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;

            this.listView1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

            this.listView1.Location = new System.Drawing.Point(0, 0);

            this.listView1.Name = "listView1";

            this.listView1.Size = new System.Drawing.Size(647, 619);

            this.listView1.TabIndex = 1;

            this.listView1.UseCompatibleStateImageBehavior = false;

            this.listView1.View = System.Windows.Forms.View.Details;

            this.listView1.DoubleClick += new System.EventHandler(this.listView1_DoubleClick);

            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;

            this.splitContainer1.Location = new System.Drawing.Point(0, 0);

            this.splitContainer1.Name = "splitContainer1";

            this.splitContainer1.Panel1.Controls.Add(this.treeView1);

            this.splitContainer1.Panel2.Controls.Add(this.listView1);

            this.splitContainer1.Size = new System.Drawing.Size(888, 619);

            this.splitContainer1.SplitterDistance = 237;

            this.splitContainer1.TabIndex = 3;

            this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));

            this.imageList1.TransparentColor = System.Drawing.Color.Transparent;

            this.imageList1.Images.SetKeyName(0, "folder.png");

            this.imageList1.Images.SetKeyName(1, "page.png");

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.ClientSize = new System.Drawing.Size(888, 619);

            this.Controls.Add(this.splitContainer1);

            this.Name = "Form1";

            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

            this.Text = "资源管理器";

            this.Load += new System.EventHandler(this.Form1_Load);

            this.contextMenuStrip1.ResumeLayout(false);

            this.splitContainer1.Panel1.ResumeLayout(false);

            this.splitContainer1.Panel2.ResumeLayout(false);

            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();

            this.splitContainer1.ResumeLayout(false);

            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.TreeView treeView1;

        private System.Windows.Forms.ListView listView1;

        private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;

        private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1;

        private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem2;

        private System.Windows.Forms.ToolStripSeparator 打开目录ToolStripMenuItem;

        private System.Windows.Forms.ToolStripMenuItem 打开目录ToolStripMenuItem1;

        private System.Windows.Forms.SplitContainer splitContainer1;

        private System.Windows.Forms.ImageList imageList1;

    }

}

 

posted @ 2021-10-11 20:17  谦寻  阅读(87)  评论(0编辑  收藏  举报