• 欢迎浏览“String me = Creater\忠实的资深Linux玩家;”,请文明浏览,理性发言,有侵犯你的权益请邮件我(creater@vip.qq.com).
  • 把任何的失败都当作一次尝试,不要自卑;把所有的成功都想成是一种幸运,不要自傲。
  •    5年前 (2013-06-06)  C# |   6 条评论  12 
    文章评分 0 次,平均分 0.0
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;
    
    namespace test
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent(); fillTree(treeView1);
            }
            private void fillTree(TreeView tv)
            {
                DirectoryInfo directory;
                string sCurPath = ""; // 重新清空 
                tv.Nodes.Clear();
    
                // 将硬盘上的所有的驱动器都列举出来 
                foreach (char c in driveLetters)
                {
                    sCurPath = c + ":\\";
                    try
                    {
                        // 获得该路径的目录信息 
                        directory = new DirectoryInfo(sCurPath);
    
                        // 如果获得的目录信息正确,则将它添加到目录树视中 
                        if (directory.Exists == true)
                        {
                            TreeNode newNode = new TreeNode(directory.FullName);
                            tv.Nodes.Add(newNode); // 添加新的节点到根节点 
                            getSubDirs(newNode);
                            // 调用getSubDirs()函数,检查该驱动器上的任何存在子目录 
                        }
                    }
                    catch (Exception doh)
                    {
                        Console.WriteLine(doh.Message);
                    }
                }
            }
    
            private void getSubDirs(TreeNode parent)
            {
                DirectoryInfo directory;
                try
                {
                    // 如果还没有检查过这个文件夹,则检查之 
                    if (parent.Nodes.Count == 0)
                    {
                        directory = new DirectoryInfo(parent.FullPath);
                        foreach (DirectoryInfo dir in directory.GetDirectories())
                        {
                            // 新建一个数节点,并添加到目录树视 
                            TreeNode newNode = new TreeNode(dir.Name);
                            parent.Nodes.Add(newNode);
                        }
                    }
    
                    foreach (TreeNode node in parent.Nodes)
                    {
                        // 如果还没有检查过这个文件夹,则检查 
                        if (node.Nodes.Count == 0)
                        {
                            directory = new DirectoryInfo(node.FullPath);
    
                            // 检查该目录上的任何子目录 
                            foreach (DirectoryInfo dir in directory.GetDirectories())
                            {
                                // 新建一个数节点,并添加到目录树视 
                                TreeNode newNode = new TreeNode(dir.Name);
                                node.Nodes.Add(newNode);
                            }
                        }
                    }
                }
                catch (Exception doh)
                {
                    Console.WriteLine(doh.Message);
                }
            }
    
            private void fillListView(ListView lv, string strPath)
            {
                DirectoryInfo directory = new DirectoryInfo(strPath);
                lv.Items.Clear();
                foreach (DirectoryInfo dir in directory.GetDirectories())
                {
                    ListViewItem item = new ListViewItem(dir.Name);
                    item.SubItems.Add(string.Empty);
                    item.SubItems.Add("文件夹");
                    item.SubItems.Add(string.Empty);
                    lv.Items.Add(item);
                }
                foreach (FileInfo file in directory.GetFiles())
                {
                    ListViewItem item = new ListViewItem(file.Name);
                    item.SubItems.Add((file.Length / 1024).ToString() + " KB");
                    item.SubItems.Add(file.Extension + "文件");
                    item.SubItems.Add(file.LastWriteTime.ToString());
                    lv.Items.Add(item);
                }
            }
    
            private string fixPath(TreeNode node)
            {
                string sRet = "";
                try
                {
                    sRet = node.FullPath;
                    int index = sRet.IndexOf("\\\\");
                    if (index > 1)
                    {
                        sRet = node.FullPath.Remove(index, 1);
                    }
                }
                catch (Exception doh)
                {
                    Console.WriteLine(doh.Message);
                }
                return sRet;
            }
    
            private void treeView1_BeforeSelect(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
            {
                getSubDirs(e.Node); // 取得选择节点的子文件夹 
                textBox1.Text = fixPath(e.Node); // 更新文本框内容 
                folder = new DirectoryInfo(e.Node.FullPath); // 获得它的目录信息
                fillListView(listView1, fixPath(e.Node));
            }
    
            private void treeView1_BeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
            {
                getSubDirs(e.Node); // 取得选择节点的子文件夹 
                textBox1.Text = fixPath(e.Node); // 更新文本框内容 
                folder = new DirectoryInfo(e.Node.FullPath); // 获得它的目录信息 
            }
        }
    
    }
    
    
    using System;
    using System.Text;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.IO;
    using System.Windows.Forms;
    using System.Data;
    using System.Xml;
    using System.Runtime.InteropServices;
    namespace test
    {
        /// <summary>
        /// Form1 的摘要说明。
        /// </summary>
        public partial class  Form1 : System.Windows.Forms.Form
        {
            private System.Windows.Forms.TreeView treeView1;
            private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.ListView listView1;
            private System.Windows.Forms.ColumnHeader columnHeader1;
            private System.Windows.Forms.ColumnHeader columnHeader2;
            private System.Windows.Forms.ColumnHeader columnHeader3;
            private System.Windows.Forms.ColumnHeader columnHeader4;
            private System.Windows.Forms.Splitter splitter1;
    
            private DirectoryInfo folder; // 用于保存目录信息 
            private static string driveLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            private System.Windows.Forms.MainMenu mainMenu1;
            private System.Windows.Forms.MenuItem menuItem1;
            private System.Windows.Forms.MenuItem menuItem2;
            private System.Data.Odbc.OdbcConnection odbcConnection1;     // 用于列举驱动器盘符 
    
            /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    if (components != null)
                    {
                        components.Dispose();
                    }
                }
                base.Dispose(disposing);
            }
    
            #region Windows 窗体设计器生成的代码
            /// <summary>
            /// 设计器支持所需的方法 - 不要使用代码编辑器修改
            /// 此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.components = new System.ComponentModel.Container();
                this.treeView1 = new System.Windows.Forms.TreeView();
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.listView1 = new System.Windows.Forms.ListView();
                this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
                this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
                this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
                this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
                this.splitter1 = new System.Windows.Forms.Splitter();
                this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
                this.menuItem1 = new System.Windows.Forms.MenuItem();
                this.menuItem2 = new System.Windows.Forms.MenuItem();
                this.odbcConnection1 = new System.Data.Odbc.OdbcConnection();
                this.SuspendLayout();
                // 
                // 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(200, 573);
                this.treeView1.TabIndex = 0;
                this.treeView1.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
                this.treeView1.BeforeSelect += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeSelect);
                // 
                // textBox1
                // 
                this.textBox1.Dock = System.Windows.Forms.DockStyle.Top;
                this.textBox1.Location = new System.Drawing.Point(200, 0);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(592, 21);
                this.textBox1.TabIndex = 1;
                this.textBox1.Text = "textBox1";
                // 
                // listView1
                // 
                this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
                this.columnHeader1,
                this.columnHeader2,
                this.columnHeader3,
                this.columnHeader4});
                this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
                this.listView1.Location = new System.Drawing.Point(200, 21);
                this.listView1.Name = "listView1";
                this.listView1.Size = new System.Drawing.Size(592, 552);
                this.listView1.TabIndex = 2;
                this.listView1.UseCompatibleStateImageBehavior = false;
                this.listView1.View = System.Windows.Forms.View.Details;
                // 
                // columnHeader1
                // 
                this.columnHeader1.Text = "名称";
                this.columnHeader1.Width = 120;
                // 
                // columnHeader2
                // 
                this.columnHeader2.Text = "大小";
                this.columnHeader2.Width = 100;
                // 
                // columnHeader3
                // 
                this.columnHeader3.Text = "类型";
                this.columnHeader3.Width = 120;
                // 
                // columnHeader4
                // 
                this.columnHeader4.Text = "时间";
                this.columnHeader4.Width = 140;
                // 
                // splitter1
                // 
                this.splitter1.Location = new System.Drawing.Point(200, 21);
                this.splitter1.Name = "splitter1";
                this.splitter1.Size = new System.Drawing.Size(3, 552);
                this.splitter1.TabIndex = 3;
                this.splitter1.TabStop = false;
                // 
                // mainMenu1
                // 
                this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                this.menuItem1});
                // 
                // menuItem1
                // 
                this.menuItem1.Index = 0;
                this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                this.menuItem2});
                this.menuItem1.Text = "文件";
                // 
                // menuItem2
                // 
                this.menuItem2.Index = 0;
                this.menuItem2.Text = "新建";
                this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
                // 
                // Form1
                // 
                this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
                this.ClientSize = new System.Drawing.Size(792, 573);
                this.Controls.Add(this.splitter1);
                this.Controls.Add(this.listView1);
                this.Controls.Add(this.textBox1);
                this.Controls.Add(this.treeView1);
                this.Menu = this.mainMenu1;
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
            #endregion
    
            private IContainer components;
        }
    }
    
     

    除特别注明外,本站所有文章均为String me = "Creater\忠实的资深Linux玩家";原创,转载请注明出处来自http://unix8.net/home.php/1399.html

    关于

    发表评论

    暂无评论

    切换注册

    登录

    忘记密码 ?

    切换登录

    注册

    扫一扫二维码分享