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

📄 form1.cs

📁 C#编程百例,是一个入门的学习好帮助,有兴趣的可以下回去看看!
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace dirTreeView
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
        private System.Windows.Forms.TreeView treeView1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.ImageList imageList1;
        private System.ComponentModel.IContainer components;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (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.components = new System.ComponentModel.Container();
            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
            this.imageList1 = new System.Windows.Forms.ImageList(this.components);
            this.button1 = new System.Windows.Forms.Button();
            this.treeView1 = new System.Windows.Forms.TreeView();
            this.SuspendLayout();
            // 
            // imageList1
            // 
            this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
            this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
            this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
            this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(216, 336);
            this.button1.Name = "button1";
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            // 
            // treeView1
            // 
            this.treeView1.ImageList = this.imageList1;
            this.treeView1.Name = "treeView1";
            this.treeView1.Size = new System.Drawing.Size(376, 336);
            this.treeView1.TabIndex = 0;
            this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(496, 365);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.button1,
                                                                          this.treeView1});
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

        private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            // check to see if the selected node is already populated
            if (e.Node.Nodes.Count > 0 || e.Node.ImageIndex == 2)
            {
                return;
            }

            // The node is not  populated, so populate the selected node

            PopulateNode(e.Node);


        }

        protected void PopulateNode(TreeNode aNode)
        {
            this.Cursor = Cursors.WaitCursor;  // Set up the wait cursor 
            string strDir = BuildDirectoryInfo(aNode);  // Build the DirectoryInfo tree string from the tree node
            DirectoryInfo Dir = new DirectoryInfo(strDir);  // Use the DirectoryInfo class to get the files and DirectoryInfo names in the DirectoryInfo
            int count = 0;

            // Get each DirectoryInfo name and add it as a DirectoryInfo folder tree node
            for (int i = 0; i < Dir.GetDirectories().Length; i++)    
            {
                TreeNode ChildNode = new TreeNode(Dir.GetDirectories()[i].Name, 1,0);
                aNode.Nodes.Add(ChildNode);
                count++;
            }

            // Get each file name and add it as a file tree node
            for (int i = 0; i < Dir.GetFiles("*.*").Length; i++)
            {
                TreeNode ChildNode = new TreeNode(Dir.GetFiles("*.*")[i].Name, 2,2);
                aNode.Nodes.Add(ChildNode);
            }

            aNode.Expand();  // show the children nodes
            this.Cursor = Cursors.Arrow;  // restore the cursor

        }

        protected string BuildDirectoryInfo(TreeNode aNode)
        {
            TreeNode theNode = aNode;
            string strDir = ""; 
            // Cycle through the node and all its parents to build the full path of the DirectoryInfo

            while (theNode != null)
            {
                if (theNode.Text[theNode.Text.Length - 1] != '\\')
                {
                    strDir = "\\" + strDir;
                }

                strDir = theNode.Text + strDir;
                theNode = theNode.Parent;
            }

            return strDir;

        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            PopulateNode(new TreeNode("c:"));
        }


	}
}

⌨️ 快捷键说明

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