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

ooyyi6 2012-12-22 10:24:18
比如我要显示D:\test\下的所有文件,最好也能显示图标~然后显示了之后还可以双击文件并且打开他~急求大神~
本人初学C#不太懂网上写的什么Shell之类的~请大牛指教
...全文
480 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
失落的神庙 2012-12-24
  • 打赏
  • 举报
回复
引用 3 楼 ooyyi6 的回复:
引用 楼主 ooyyi6 的回复:比如我要显示D:\test\下的所有文件,最好也能显示图标~然后显示了之后还可以双击文件并且打开他~急求大神~ 本人初学C#不太懂网上写的什么Shell之类的~请大牛指教 用listview显示~
。。。用listview就是不行了么?这里只告诉方法 不帮忙写代码的
ooyyi6 2012-12-24
  • 打赏
  • 举报
回复
引用 楼主 ooyyi6 的回复:
比如我要显示D:\test\下的所有文件,最好也能显示图标~然后显示了之后还可以双击文件并且打开他~急求大神~ 本人初学C#不太懂网上写的什么Shell之类的~请大牛指教
用listview显示~
失落的神庙 2012-12-22
  • 打赏
  • 举报
回复
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);
                            }
                        }
                    }
                }

            }
        }

目录树形结构显示。 路径知道了。。。打开文件就不用说了哈
bdmh 2012-12-22
  • 打赏
  • 举报
回复

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧