Traversing file directories with C++builder

Source: Internet
Author: User

Using C++builder, we can easily implement a directory tree of Windows-like explorer. Ok,let ' s go.

We need a ttree and a timagelist control. How do I get the system's drive letter? Borland gives us a tdrivetype class, what? Help can not find, oh, this is the implementation of a class used Tdirectorycombobox, the help is of course not, it is defined as follows:

typedef enum ATTR { atDirectory,atFile} Attr;
struct ITEMPROPERTY
{
Attr ItemAttr;
}ItemProperty;
typedef enum TDRIVETYPE {dtUnknown, dtNoDrive, dtFloppy, dtFixed, dtNetwork, dtCDROM,dtRAM}TDriveType;

So how do you know if these are floppy drives or hard disks? This requires the use of the WINAPI function GetDriveType. The definition is as follows:

UINT GetDriveType (LPCTSTR lprootpathname); For details please refer to the Help document, I will not be here to delay everyone's time. Finally, we use the BCB findfirst,findnext function to achieve the directory traversal.

The following two functions Initdriectorytree are used to obtain the disk characters and their properties, browsedriectory the recursive method to achieve the directory traversal, specifically implemented as follows:

BOOL __fastcall Tform1::initdirectorytree ()
{
Drivecount=getalldrive ();
Drivetype=new Tdrivetype[4];
for (int i=drivecount;i>0;i--)
{
ttreenode* node=dirtree->items->item[i];
Drivetype[i]=tdrivetype (GetDriveType (Node->text.c_str ()));
Switch (Drivetype[i])
{
Case Dtfloppy://Floppy drive
Case Dtfixed:browsedirectory (node->text,node); break;//Hard Drive
Case Dtnetwork:browsedirectory (node->text,node); break;//Network Drive
Case DTCDROM://Optical drive
Case Dtram:browsedirectory (Node->text,node); Memory virtual disk? Not quite clear, probably like the E: Disk generated by the Win98 boot disk
};
}
return true;
Todo:add Your source code here
}
//---------------------------------------------------------------------------
void __fastcall tform1::browsedirectory (ansistring Dir, ttreenode* Node)
{
Tsearchrec SR;
ttreenode* Subnode=node;
if (FindFirst (dir+ "*.*", FADIRECTORY,SR) ==0)
while (1)
{if (Sr. ATTR==FADIRECTORY&&SR. Name!= "." &&SR. Name!= "..")
{
Subnode=dirtree->items->addchild (NODE,SR. Name);
Browsedirectory (DIR+SR. Name+ "\", subnode);
}
if (FindNext (SR)!=0) break;
}
FindClose (SR);
Return
Todo:add Your source code here
}
//---------------------------------------------------------------------------

Above we use C++builder to achieve the access to the computer file system, in fact, we can use the above function to create a file system can access the VCL control to replace the BCB provided Tdirectorylistbox and Tdirectorycombobox, Make your program more Win9x style.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.