日期:2014-05-18  浏览次数:20956 次

c# 動態的生成一個樹.
c# 動態的生成一個樹,根據文件名(文件不是固定的,是動態獲得的)树的节点名字是此文件下的子文件的名字,树的内容对应文件的一个层次。謝謝!

------解决方案--------------------
http://blog.csdn.net/zgke/archive/2008/11/29/3408133.aspx

看看这个 可能对你有帮助
------解决方案--------------------
顶顶
------解决方案--------------------
C# code

 private void Form1_Load(object sender, EventArgs e)
        {
            imageList1.Images.Clear();
            treeView1.Nodes.Clear();
            foreach (string cpname in Directory.GetLogicalDrives())
            {
                if (cpname == "C:\\")
                {
                    continue;
                }

                Win32.SHFILEINFO shfi = new Win32.SHFILEINFO();

                DirectoryInfo dir = new DirectoryInfo(cpname);
                //获得图标
                Win32.SHGetFileInfo(cpname,
                                    (uint)0x80,
                                    ref shfi,
                                    (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
                                    (uint)(0x100 | 0x400)); //取得Icon和TypeName
                //添加图标
                imageList1.Images.Add(dir.Name, (Icon)Icon.FromHandle(shfi.hIcon).Clone());
                TreeNode node = new TreeNode(dir.Name);
                node.ImageIndex = imageList1.Images.Count - 1;
                node.SelectedImageIndex = imageList1.Images.Count - 1;
                node.Name = dir.FullName;
                treeView1.Nodes.Add(node);
                Win32.DestroyIcon(shfi.hIcon);

                node.Nodes.Add(lsnode);
                node.Tag = true;
            }

            treeView1.CollapseAll();
        }

/// <summary>
        /// 针对文件目录
        /// </summary>
        private void GetListViewItem(string path, TreeNode snode)
        {
            string[] dirs = Directory.GetDirectories(path);
            string[] files = Directory.GetFiles(path);
            Win32.SHFILEINFO shfi = new Win32.SHFILEINFO();
            for (int i = 0; i < dirs.Length; i++)
            {
                try
                {
                    DirectoryInfo dir = new DirectoryInfo(dirs[i]);
                    if (dir.Name == "auto.exe" || dir.Name == "Autorun.inf" || dir.Name == "RECYCLER" ||
                        dir.Name == "System Volume Information" || dir.Name == "$RECYCLE.BIN")
                        continue;

                    //获得图标
                    Win32.SHGetFileInfo(dirs[i],
                                        (uint)0x80,
                                        ref shfi,
                                        (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
                                        (uint)(0x100 | 0x400)); //取得Icon和TypeName
                    //添加图标
                    imageList1.Images.Add(dir.Name, (Icon)Icon.FromHandle(shfi.hIcon).Clone());
                    TreeNode node = new TreeNode(dir.Name);
                    node.Name = dir.FullName;
                    node.ImageIndex = imageList1.Images.Count - 1;
                    node.SelectedImageIndex = imageList1.Images.Count - 1;
                    node.Tag = true;
                    snode.Nodes.Add(node);
                    node.Nodes.Add(lsnode);
                    Win32.DestroyIcon(shfi.hIcon);
                }
                catch
                {
                }
            }
            for (int i = 0; i < files.Length; i++)
            {
                try
                {
                    FileInfo fi = new FileInfo(files[i]);
                    //获得图标
                    Win32.SHGetFileInfo(files[i],
                                        (uint)0x80,
                                        ref shfi,
                                        (uint)System.Runtime.InteropServices.Marshal.SizeOf