获取所有逻辑磁盘目录

Posted on 2019-01-09 19:09  努力成长静待花开  阅读(362)  评论(0编辑  收藏  举报

实现效果:

  

知识运用:

  Directory类的GetLogicalDrives方法    //检索计算机上的逻辑分区

  public static string[] GetLogicalDrives()

  GetDirectories方法    和GetFiles方法

实现代码:

        public void GetListViewItem(string path,ImageList imagelist,ListView lv)
        {
            lv.Items.Clear();
            SHFILEINFO shfi = new SHFILEINFO();
            try
            {
                string[] dirs = Directory.GetDirectories(path);
                string[] files = Directory.GetFiles(path);
                for (int i = 0; i < dirs.Length; i++)
                { 
                    string[] info=new string[4];
                    DirectoryInfo dir = new DirectoryInfo(dirs[i]);
                    if (!(dir.Name == "RECYCLER" || dir.Name == "RECYCLED" ||
                        dir.Name == "Recycled" ||dir.Name == "System Volume Information"))
                    { 
                        SHGetFileInfo(dirs[i], 
                                    (uint)0x80, ref shfi, 
                                    (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
                                    (uint)(0x100 | 0x400));
                        imagelist.Images.Add(dir.Name,(Icon)Icon.FromHandle(shfi.hIcon).Clone());
                        info[0] = dir.Name;
                        info[1] = "";
                        info[2] = "文件夹";
                        info[3] = dir.LastAccessTime.ToString();
                        ListViewItem item = new ListViewItem(info,dir.Name);
                        lv.Items.Add(item);
                    }
                }
                for (int i = 0; i < files.Length; i++)
                {
                    string[] info = new string[4];
                    FileInfo fi = new FileInfo(files[i]);
                    string Newtype=fi.Extension.ToLower();
                    if (!(Newtype == "sys" || Newtype == "ini" || Newtype == "bin" ||
                            Newtype == "log" || Newtype == "com" || Newtype == "bat" || Newtype == "db"))
                    {
                        SHGetFileInfo(files[i],
                                    (uint)0x80, ref shfi,
                                    (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
                                    (uint)(0x100 | 0x400));
                        imagelist.Images.Add(fi.Name,(Icon)Icon.FromHandle(shfi.hIcon));
                        info[0] = fi.Name;
                        info[1] = fi.Length.ToString();
                        info[2] = fi.Extension.ToString();
                        info[3] = fi.LastAccessTime.ToString();
                        ListViewItem item = new ListViewItem(info,fi.Name);
                        lv.Items.Add(item);
                    }
                }
            }
            catch {}
        }