Traverse and load the specified directory in C ++ Builder

zhaozj2021-02-08  268

Home: http://www.maxss.net email: maxchou@163.com

When we write an app, sometimes you will encounter the traversal of the directory, hereby introducing the principles and code of the procedure for the project of the Plaphic Directory when developing the WML Microwser Favorites.

In this program, the following controls are used to use the WinAPI function:

TTREEVIEW component

FINDFIRST ()

FINDNEXT ()

FindClose ()

GetFileAttributes ()

Package the traversal directory and generated tree node to the following functions:

Void __fastcall tform1 :: createfavoritetree (Ansistring SourcePath, TTREENODE * NODE);

parameter:

SourcePath: Specifies the directory that needs to be traversed, such as "C: // myfolder //"

Node: Which of the generated tree nodes is loaded under TTREEVIEW?

Functional body:

TsearchRec Sr;

IATTRIBUTES = 0;

DWord Resultattr;

Ansistring FoundFileName;

attributes | = faanyfile;

Try

{

TTREENODE * PNODE;

IF (FindFirst (SourcePath "*. *", IATTRIBUTES, SR) == 0)

{

Do // Start looking for files

{

FoundFileName = SourcePath sr.name; // Generate Founded Path

Resultattr = getFileAttributes (LPCTSTR (FoundFileName.c_STR ())); // Return to file properties

// Create a directory node

If (resultttr == file_attribute_directory) // found file is a directory type

{

IF (sr.name.pos (") == 0) // Filter off the default subdirectory". "and" .. "

{

PNODE = tvfav-> items-> addchild (node, sr.name);

PNODE-> imageIndex = 0;

Pnode-> SELECTEDINDEX = 0;

CreateFavoritetree (SourcePath Sr.Name "//", PNODE);

}

} // end if (sr.name.pos ...

// Create a file node

IF (resultttr! = file_attribute_directory)

{

IF (excitfileext (sr.name) == ".wurl")

{

Pnode = tvfav-> items-> addchild (node

Sr.name.substring (0, sr.name.length () -

Extractfileext (sr.name) .length ()));

PNODE-> imageIndex = 1;

Pnode-> SELECTEDINDEX = 1;

}

}

} while (FindNext (SR) == 0);

FindClose (SR); // Close

}

}

Catch (...)

{

MessageBox (this-> handle, "Read Folder Failed!", "Error", MB_OK | MB_ICONERROR); RETURN FALSE

}

Return True;

Call method:

Void __fastcall tfrmmanagefav :: formactivate (TOBJECT * Sender)

{

Tvfav-> items-> clean ();

TTREENODE * PNODE = TVFAV-> Items-> Add (Null, "Favorite");

CreateFavoritetree (Application-> Exename) "Favorite //", PNODE);

Tvfav-> fullxpand ();

}

This allows the directory structure under the subdirectory "Favorite" in the application directory to the TTREWVIEW control, which is easy to implement your specific needs by modifying the above code.

转载请注明原文地址:https://www.9cbs.com/read-2181.html

New Post(0)