xtd 0.2.0
Loading...
Searching...
No Matches

◆ enumerate_files() [1/2]

xtd::io::directory_info::file_iterator xtd::io::directory_info::enumerate_files ( ) const

Returns an enumerable collection of file information in the current directory.

Returns
An xtd::io::directory_info::file_iterator of the files in the current directory.
Exceptions
xtd::io::directory_not_found_exceptionThe specified path is invalid, such as being on an unmapped drive.
xtd::security::security_exceptionThe caller does not have code access permission to create the directory.
Examples
The following example enumerates the files under a specified directory.

If you only need the names of the files, use the static xtd::io::directory class for better performance. For an example, see the xtd::io::directory::enumerate_files method.

#include <xtd/xtd>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static auto main() {
// Create a directory_info of the directory of the files to enumerate.
directory_info dir_info(R"(\\archives1\library\)");
// Get the files iteror.
auto files = dir_info.enumerate_files();
// Show results.
for (auto f : files) {
console::write_line("{0}", f.name());
}
}
};
startup_(program::main);
Exposes instance methods for creating, moving, and enumerating through directories and subdirectories...
Definition directory_info.h:129
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:175
@ f
The F key.
The xtd::io namespace contains types that allow reading and writing to files and data streams,...
Definition binary_reader.h:16
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Examples
The following example shows how to enumerate files in a directory by using different search options. The example assumes a directory that has files named log1.txt, log2.txt, test1.txt, test2.txt, test3.txt.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static auto main() {
directory_info di(R"(C:\ExampleDir)");
console::write_line("No search pattern returns:");
for (auto fi : di.enumerate_files()) {
console::write_line(fi.name());
}
console::write_line();
console::write_line("Search pattern *2* returns:");
for (auto fi : di.enumerate_files("*2*")) {
console::write_line(fi.name());
}
console::write_line();
console::write_line("Search pattern test?.txt returns:");
for (auto fi : di.enumerate_files("test?.txt")) {
console::write_line(fi.name());
}
}
};
startup_(program::main);
/*
This code produces output similar to the following:
No search pattern returns:
log1.txt
log2.txt
test1.txt
test2.txt
test3.txt
Search pattern *2* returns:
log2.txt
test2.txt
Search pattern test?.txt returns:
test1.txt
test2.txt
test3.txt
*&zwj;/
Remarks
The xtd::io::directory_info::enumerate_files and xtd::io::directory_info::get_files methods differ as follows:
Therefore, when you are working with many files and directories, xtd::io::directory_info::enumerate_files can be more efficient.
This method pre-populates the values of the following xtd::io::directory_info properties: