当前位置:编程学习 > C#/ASP.NET >>

C#中怎么用listview显示指定路径下的文件和文件夹~

比如我要显示D:\test\下的所有文件,最好也能显示图标~然后显示了之后还可以双击文件并且打开他~急求大神~
本人初学C#不太懂网上写的什么Shell之类的~请大牛指教 --------------------编程问答-------------------- 用什么shell,不就是目录遍历吗http://jlj84237485.blog.163.com/blog/static/34929460201010401434877/ --------------------编程问答--------------------
List<ProgramInfo> ProInfo = new List<ProgramInfo>();
            Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall", false);
            Microsoft.Win32.RegistryKey key2 = null;
            string softwareName, installLocation, IconPath;
            if (key != null)//判断对象存在
            {
                foreach (string keyName in key.GetSubKeyNames())//遍历子项名称的字符串数组
                {
                    key2 = key.OpenSubKey(keyName, false);//遍历子项节点

                    if (key2 != null)
                    {
                        softwareName = key2.GetValue("DisplayName", "").ToString();//获取软件名
                        IconPath = key2.GetValue("DisplayIcon", "").ToString();//获取安装路径InstallLocation
                        installLocation = key2.GetValue("InstallLocation", "").ToString();
                        if (!string.IsNullOrEmpty(installLocation))
                        {
                            ProInfo.Add(new ProgramInfo()
                            {
                                ProName = softwareName,
                                ProPath = installLocation,
                                ProIconPath = IconPath
                            });
                        }
                        softwareName = "";
                        installLocation = "";
                    }
                    key2 = null;
                }
            }
            for (int i = 0; i < ProInfo.Count; i++)
            {
                try
                {
                    ProInfo[i].ProImage = System.Drawing.Icon.ExtractAssociatedIcon(ProInfo[i].ProIconPath);
                }
                catch { ProInfo[i].ProImage = null; }
            }
            int indexI = 0;
            imageList1.ImageSize = new Size(32, 32);
            imageList1.ColorDepth = ColorDepth.Depth32Bit; 
            foreach (ProgramInfo i in ProInfo.ToArray())
            {
                if (i.ProImage != null)
                {
                    try
                    {
                        ListViewItem item = new ListViewItem(i.ProName);
                        imageList1.Images.Add(i.ProImage.ToBitmap());
                        item.ImageIndex = indexI;
                        item.BackColor = Color.Transparent;
                        listView1.Items.Add(item);
                        item.SubItems.Add("");
                        item.SubItems.Add("");
                        indexI++;
                    }
                    catch (Exception ex) { string e = ex.ToString(); }
                }
            }
            listView1.LargeImageList = imageList1;
            pictureBox1.Image = ProInfo[0].ProImage.ToBitmap();




 class ProgramInfo
        {
            public string ProName;
            public string ProPath;
            public string ProIconPath;
            public Icon ProImage;
        }


安装程序图标获取和显示


private void BindDrives()
        {
            DriveInfo[] drvs = DriveInfo.GetDrives();
            foreach (DriveInfo drv in drvs)
            {
                TreeNode root = new TreeNode();
                root.Text = drv.Name;
                root.Tag = drv.RootDirectory.ToString();
                // root.Nodes.Add("");
                treeView1.Nodes.Add(root);
                if (Directory.Exists(drv.RootDirectory.ToString()))
                {
                    DirectoryInfo dInfo = new DirectoryInfo(drv.RootDirectory.ToString());

                    FileSystemInfo[] files = dInfo.GetFileSystemInfos();
                    if (files.Length > 0) //有子节点,先添加一个空节点
                    {
                        root.Nodes.Add("emptyNode", string.Empty);
                    }
                }
            }
        }

        //展开节点,移除以前的空节点,加载子节点
        private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
        {
            TreeNode parentNode = e.Node;
            // parentNode.Nodes.RemoveByKey("emptyNode");//移除空节点
            parentNode.Nodes.Clear();
            string path = parentNode.Tag.ToString();

            if (Directory.Exists(path))
            {
                DirectoryInfo dir = new DirectoryInfo(path);

                FileSystemInfo[] files = dir.GetFileSystemInfos();
                foreach (FileSystemInfo f in files)
                {
                    TreeNode node = new TreeNode();
                    node.Text = f.Name;
                    node.Tag = f.FullName;
                    parentNode.Nodes.Add(node);  //加载子节点

                    if (Directory.Exists(node.Tag.ToString()))
                    {
                        DirectoryInfo subDir = new DirectoryInfo(node.Tag.ToString());
                        if (subDir.Attributes != (FileAttributes.System | FileAttributes.Hidden | FileAttributes.Directory))
                        {
                            FileSystemInfo[] subFiles = null;
                            try
                            {
                                subFiles = subDir.GetFileSystemInfos();
                            }
                            catch { continue; }
                            if (subFiles.Length > 0)   //有子节点,先添加一个空节点
                            {
                                node.Nodes.Add("emptyNode", string.Empty);
                            }
                        }
                    }
                }

            }
        }



目录树形结构显示。

路径知道了。。。打开文件就不用说了哈 --------------------编程问答--------------------
引用 楼主 ooyyi6 的回复:
比如我要显示D:\test\下的所有文件,最好也能显示图标~然后显示了之后还可以双击文件并且打开他~急求大神~
本人初学C#不太懂网上写的什么Shell之类的~请大牛指教

用listview显示~ --------------------编程问答--------------------
引用 3 楼 ooyyi6 的回复:
引用 楼主 ooyyi6 的回复:比如我要显示D:\test\下的所有文件,最好也能显示图标~然后显示了之后还可以双击文件并且打开他~急求大神~
本人初学C#不太懂网上写的什么Shell之类的~请大牛指教
用listview显示~



。。。用listview就是不行了么?这里只告诉方法
不帮忙写代码的
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,