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

◆ get_file_system_infos() [1/2]

std::vector< xtd::sptr< xtd::io::file_system_info > > xtd::io::directory_info::get_file_system_infos ( ) const

Returns an array of strongly typed xtd::io::file_system_info entries representing all the files and subdirectories in a directory.

Returns
An array of strongly typed xtd::io::file_system_info entries.
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 counts the files and directories under the specified directory.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static auto main() {
try {
console::write_line("Enter the path to a directory:");
string directory = console::read_line();
// Create a new directory_info object.
if (!dir.exists()) {
throw directory_not_found_exception("The directory does not exist.", csf_);
}
// Call the GetFileSystemInfos method.
std::vector<xtd::sptr<file_system_info>> infos = dir.get_file_system_infos();
console::write_line("Working...");
// Pass the result to the list_directories_and_files
// method defined below.
list_directories_and_files(infos);
// Display the results to the console.
console::write_line("Directories: {0}", directories);
console::write_line("Files: {0}", files);
} catch (const exception& e) {
console::write_line(e.message());
}
}
private:
static void list_directories_and_files(std::vector<xtd::sptr<file_system_info>> fs_info) {
// Iterate through each item.
for (xtd::sptr<file_system_info> i : fs_info) {
// Check to see if this is a directory_info object.
if (is<directory_info>(i)) {
// Add one to the directory count.
directories++;
// Cast the object to a directory_info object.
xtd::sptr<directory_info> d_info = as<directory_info>(i);
// Iterate through all sub-directories.
list_directories_and_files(d_info->get_file_system_infos());
}
// Check to see if this is a FileInfo object.
else if (is<file_info>(i)) {
// Add one to the file count.
files++;
}
}
}
inline static long files = 0;
inline static long directories = 0;
};
startup_(program::main);
Defines the base class for predefined exceptions in the xtd namespace.
Definition exception.h:26
Exposes instance methods for creating, moving, and enumerating through directories and subdirectories...
Definition directory_info.h:129
The exception that is thrown when part of a file or directory cannot be found.
Definition directory_not_found_exception.h:29
Exposes static methods for creating, moving, and enumerating through directories and subdirectories....
Definition directory.h:101
#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
#define csf_
Provides information about the current stack frame.
Definition current_stack_frame.h:30
std::shared_ptr< type_t > sptr
The xtd::sptr object is a shared pointer.
Definition sptr.h:25
@ e
The E 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
Remarks
The xtd::io::directory_info::enumerate_file_system_infos and xtd::io::directory_info::get_file_system_infos methods differ as follows:
Therefore, when you are working with many files and directories, xtd::io::directory_info::enumerate_file_system_infos can be more efficient.
This method pre-populates the values of the following xtd::io::file_system_info properties: