1. 程式人生 > >獲取所有邏輯磁盤目錄

獲取所有邏輯磁盤目錄

icon cycle ive last file tlist 檢索 format bat

實現效果:

  技術分享圖片

知識運用:

  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 {}
        }

  

獲取所有邏輯磁盤目錄