xtd 0.2.0
Loading...
Searching...
No Matches
xtd::io::directory Class Reference
Inheritance diagram for xtd::io::directory:
xtd::static_object

Definition

Exposes static methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited.

Exposes static methods for creating, moving, and enumerating through directories and subdirectories....
Definition directory.h:99
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition static.h:37
#define core_export_
Define shared library export.
Definition core_export.h:13
Inheritance
xtd::static_objectxtd::io::directory
Header
#include <xtd/io/directory>
Namespace
xtd::io
Library
xtd.core
Examples
The following example shows how to retrieve all the text files from a directory and move them to a new directory. After the files are moved, they no longer exist in the original directory.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static auto main() {
ustring source_directory = R"(C:\current)";
ustring archive_directory = R"(C:\archive)";
try {
auto txt_files = directory::enumerate_files(source_directory, "*.txt");
for (ustring current_file : txt_files) {
ustring file_name = current_file.substring(source_directory.size() + 1);
directory::move(current_file, path::combine(archive_directory, file_name));
}
} catch (system_exception& e) {
console::write_line(e.message());
}
}
};
startup_(program::main);
Defines the base class for predefined exceptions in the xtd namespace.
Definition system_exception.h:25
Represents text as a sequence of UTF-8 code units.
Definition ustring.h:47
ustring substring(size_t start_index) const noexcept
Retrieves a substring from this instance. The substring starts at a specified character position and ...
#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:166
@ 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
Examples
The following example demonstrates how to move a directory and all its files to a new directory. The original directory no longer exists after it has been moved.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static auto main() {
ustring source_directory = R"(C:\source)";
ustring destination_directory = R"(C:\destination)";
try {
directory::move(source_directory, destination_directory);
} catch (const system_exception& e) {
console::write_line(e.message());
}
}
};
startup_(program::main);
Remarks
Use the xtd::io::directory class for typical operations such as copying, moving, renaming, creating, and deleting directories.
The static methods of the xtd::io::directory class perform security checks on all methods. If you are going to reuse an object several times, consider using the corresponding instance method of xtd::io::directory_info instead, because the security check will not always be necessary.
If you are performing only one directory-related action, it might be more efficient to use a static xtd::io::directory method rather than a corresponding xtd::io::directory_info instance method. Most xtd::io::directory methods require the path to the directory that you are manipulating.
Note
In members that accept a string path parameter, that path must be well-formed or an exception is raised. For example, if a path is fully qualified but begins with a space (" c:\temp"), the path string isn't trimmed, so the path is considered malformed and an exception is raised. In addition, a path or a combination of paths cannot be fully qualified twice. For example, "c:\temp c:\windows" also raises an exception. Ensure that your paths are well-formed when using methods that accept a path string. For more information see xtd::io::path.
Remarks
In members that accept a path, the path can refer to a file or a directory. You can use a full path, a relative path, or a Universal Naming Convention (UNC) path for a server and share name. For example, all the following are acceptable paths:
  • "c:\\MyDir".
  • "MyDir\\MySubdir".
  • "\\\\MyServer\\MyShare".
By default, full read/write access to new directories is granted to all users. However, the app must have the correct security to access existing directories.
To demand permissions for a directory and all its subdirectories, end the path string with the directory separator character. (For example, "C:\Temp\" grants access to C:\Temp\ and all its subdirectories.) To demand permissions only for a specific directory, end the path string with a period. (For example, "C:\Temp." grants access only to C:\Temp\, not to its subdirectories.) @remarks In members that accept a search_pattern parameter, the search string can be any combination of literal characters and two wildcard characters; * and ?. This parameter does not recognize regular expressions. For more information, see the xtd::io::directory::enumerate_directories(ustring, ustring) method or any other method that uses the search_pattern parameter. @remarks For a list of common I/O tasks, see <a href="https://gammasoft71.github.io/xtd/docs/documentation/Guides/xtd.core/Common%20I%3AO%20tasks" >Common I/O Tasks</a>. @remarks xtd::io::directory and xtd::io::directory_info are not supported for use in Windows Store apps. For information about how to access files and folders in Windows Store apps, see <a href="https://msdn.microsoft.com/library/windows/apps/hh758319.aspx" >Accessing data and files (Windows Store apps).

Classes

class  directory_iterator
 Represent directory iterator used by xtd::io::directory. More...
 
class  file_iterator
 Represent file iterator used by xtd::io::directory. More...
 
class  file_system_entry_iterator
 Represent file system iterator used by xtd::io::directory. More...
 

Public Static Methods

static xtd::io::directory_info create_directory (const xtd::ustring &path)
 Creates all directories and subdirectories in the specified path unless they already exist.
 
static xtd::io::directory::directory_iterator enumerate_directories (const xtd::ustring &path)
 Returns an enumerable collection of directory full names in a specified path.
 
static xtd::io::directory::directory_iterator enumerate_directories (const xtd::ustring &path, const xtd::ustring &search_pattern)
 Returns an enumerable collection of directory full names that match a search pattern in a specified path.
 
static xtd::io::directory::file_iterator enumerate_files (const xtd::ustring &path)
 Returns an enumerable collection of full file names in a specified path.
 
static xtd::io::directory::file_iterator enumerate_files (const xtd::ustring &path, const xtd::ustring &search_pattern)
 Returns an enumerable collection of full file names that match a search pattern in a specified path.
 
static xtd::io::directory::file_system_entry_iterator enumerate_file_system_entries (const xtd::ustring &path)
 Returns an enumerable collection of file names and directory names in a specified path.
 
static xtd::io::directory::file_system_entry_iterator enumerate_file_system_entries (const xtd::ustring &path, const xtd::ustring &search_pattern)
 Returns an enumerable collection of file names and directory names that match a search pattern in a specified path.
 
static bool exists (const xtd::ustring &path)
 Determines whether the given path refers to an existing directory on disk.
 
static xtd::date_time get_creation_time (const xtd::ustring &path)
 Gets the creation date and time of a directory.
 
static xtd::ustring get_current_directory ()
 Gets the current working directory of the application.
 
static std::vector< xtd::ustringget_directories (const xtd::ustring &path)
 Returns the names of subdirectories (including their paths) in the specified directory.
 
static std::vector< xtd::ustringget_directories (const xtd::ustring &path, const xtd::ustring &search_pattern)
 Returns the names of subdirectories (including their paths) that match the specified search pattern in the specified directory.
 
static xtd::ustring get_directory_root (const xtd::ustring &path)
 Returns the volume information, root information, or both for the specified path.
 
static std::vector< xtd::ustringget_files (const xtd::ustring &path)
 Returns the names of files (including their paths) in the specified directory.
 
static std::vector< xtd::ustringget_files (const xtd::ustring &path, const xtd::ustring &search_pattern)
 Returns the names of files (including their paths) that match the specified search pattern in the specified directory.
 
static std::vector< xtd::ustringget_file_system_entries (const xtd::ustring &path)
 Returns the names of all files and subdirectories in a specified path.
 
static std::vector< xtd::ustringget_file_system_entries (const xtd::ustring &path, const xtd::ustring &search_pattern)
 Returns an array of file names and directory names that match a search pattern in a specified path.
 
static xtd::date_time get_last_access_time (const xtd::ustring &path)
 Returns the date and time the specified file or directory was last accessed.
 
static xtd::date_time get_last_write_time (const xtd::ustring &path)
 Returns the date and time the specified file or directory was last written to.
 
static std::vector< xtd::ustringget_logical_drives ()
 Retrieves the names of the logical drives on this computer in the form "<drive letter>:\".
 
static xtd::io::directory_info get_parent (const xtd::ustring &path)
 Retrieves the parent directory of the specified path, including both absolute and relative paths.
 
static xtd::io::file_permissions get_permissions (const xtd::ustring &path)
 Gets the xtd::io::file_permissions of the directory on the path.
 
static void move (const xtd::ustring &source_dir_name, const xtd::ustring &dest_dir_name)
 Moves a file or a directory and its contents to a new location.
 
static void remove (const xtd::ustring &path)
 Deletes an empty directory from a specified path.
 
static void remove (const xtd::ustring &path, bool recursive)
 Deletes the specified directory and, if indicated, any subdirectories and files in the directory.
 
static void set_creation_time (const xtd::ustring &path, const xtd::date_time &creation_time)
 Sets the creation date and time for the specified file or directory.
 
static void set_creation_time (const xtd::ustring &path, time_t creation_time)
 Sets the creation date and time for the specified file or directory.
 
static void set_current_directory (const xtd::ustring &path)
 Sets the application's current working directory to the specified directory.
 
static void set_last_access_time (const xtd::ustring &path, const xtd::date_time &last_access_time)
 Sets the date and time the specified file or directory was last accessed.
 
static void set_last_write_time (const xtd::ustring &path, const xtd::date_time &last_write_time)
 Sets the date and time a directory was last written to.
 
static void set_permissions (const xtd::ustring &path, xtd::io::file_permissions permissions)
 Sets the specified xtd::io::file_permissions of the directory on the specified path.
 

Member Function Documentation

◆ create_directory()

static xtd::io::directory_info xtd::io::directory::create_directory ( const xtd::ustring path)
static

Creates all directories and subdirectories in the specified path unless they already exist.

Parameters
pathThe directory to create.
Returns
An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par Examples The following example creates and deletes the specified directory: @code #include <xtd/xtd> using namespace xtd; using namespace xtd::io; class program { public: static auto main() { // Specify the directories you want to manipulate. directory_info di("c:\MyDir"); try { // Determine whether the directory exists. if (di.exists()) { // Indicate that the directory already exists. console::write_line("That path exists already."); return; } // Try to create the directory. di.create(); console::write_line("The directory was created successfully."); // Delete the directory. di.remove(); console::write_line("The directory was deleted successfully."); } catch (const system_exception& e) { console::write_line("The process failed: {0}", e.to_string()); } } }; startup_(program::main); @endcode @par Examples To create the directory C:\Users\User1\Public\Html when the current directory is C:\Users\User1, use any of the following calls to ensure that the backslash is interpreted properly: @code directory::create_directory("Public\Html"); directory::create_directory("\Users\User1\Public\Html"); directory::create_directory("c:\Users\User1\Public\Html");
Remarks
Any and all directories specified in path are created, unless they already exist or unless some part of path is invalid. If the directory already exists, this method does not create a new directory, but it returns a DirectoryInfo object for the existing directory.
The path parameter specifies a directory path, not a file path.
Trailing spaces are removed from the end of the path parameter before creating the directory.
Creating a directory with only the colon character (:) is not supported, and will cause a not_supported_exception to be thrown.
On Unix systems, use a forward slash (/) as path separator.

◆ enumerate_directories() [1/2]

static xtd::io::directory::directory_iterator xtd::io::directory::enumerate_directories ( const xtd::ustring path)
static

Returns an enumerable collection of directory full names in a specified path.

Parameters
pathThe relative or absolute path to the directory to search. This string is not case-sensitive.
Returns
An xtd::io::directory::directory_iterator of the full names (including paths) for the directories in the directory specified by path.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par rExample The following example enumerates the top-level directories in a specified path. @code #include <xtd/xtd> using namespace std; using namespace xtd; using namespace xtd::io; class program { public: static auto main() { try { // Set a variable to the My Documents path. ustring doc_path = environment::get_folder_path(environment::special_folder::my_documents); vector<ustring> dirs(begin(directory::enumerate_directories(doc_path)), end(directory::enumerate_directories(doc_path))); for (auto dir : dirs) { console::write_line("{}", dir.substring(dir.last_index_of(path::directory_separator_char()) + 1)); } console::write_line("{} directories found.", dirs.size()); } catch (const unauthorized_access_exception& ex) { console::write_line(ex.message()); } catch (const path_too_long_exception& ex) { console::write_line(ex.message()); } } };

startup_(program::main);

Remarks
You can specify relative or absolute path information in the path parameter. Relative path information is interpreted as relative to the current working directory, which you can determine by using the xtd::io::directory::get_current_directory method. The returned directory names are prefixed with the value you provided in the path parameter. For example, if you provide a relative path in the path parameter, the returned directory names will contain a relative path.
The xtd::io::directory::enumerate_directories and xtd::io::directory::get_directories methods differ as follows: When you use xtd::io::directory::enumerate_directories, you can start enumerating the collection of names before the whole collection is returned; when you use xtd::io::directory::get_directories, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, xtd::io::directory::enumerate_directories can be more efficient.

◆ enumerate_directories() [2/2]

static xtd::io::directory::directory_iterator xtd::io::directory::enumerate_directories ( const xtd::ustring path,
const xtd::ustring search_pattern 
)
static

Returns an enumerable collection of directory full names that match a search pattern in a specified path.

Parameters
pathThe relative or absolute path to the directory to search. This string is not case-sensitive.
search_patternThe search string to match against the names of directories in path. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.
Returns
An xtd::io::directory::directory_iterator of the full names (including paths) for the directories in the directory specified by path and that match the specified search pattern.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par rExample The following example enumerates the top-level directories in a specified path that match a specified search pattern. @code #include <xtd/xtd> using namespace std; using namespace xtd; using namespace xtd::io; class program { public: static auto main() { try { ustring dir_path = R"(\archives\2009\reports)"; // Create a List collection. auto dirs = vector<ustring>(begin(directory::enumerate_directories(dir_path, "dv_*")), end(directory::enumerate_directories(dir_path, "dv_*"))); // Show results. for (auto dir : dirs) { // Remove path information from string. console::write_line("{0}", dir.substring(dir.last_index_of("\") + 1)); } console::write_line("{0} directories found.", dirs.size()); } catch (const unauthorized_access_exception& ex) { console::write_line(ex.message()); } catch (const path_too_long_exception& ex) { console::write_line(ex.message()); } } }; startup_(program::main); @endcode @remarks search_pattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. The following wildcard specifiers are permitted in search_pattern. <table class="markdownTable"> <tr class="markdownTableHead"> <th class="markdownTableHeadNone"> Wildcard specifier

Matches

* (asterisk)

Zero or more characters in that position.

? (question mark)

Zero or one character in that position.

Remarks
Characters other than the wildcard are literal characters. For example, the search_pattern string "*t" searches for all names in path ending with the letter "t". The search_pattern string "s*" searches for all names in path beginning with the letter "s".
search_pattern cannot end in two periods ("..") or contain two periods ("..") followed by xtd::io::path::directory_separator_char or xtd::io::path::alt_directory_separator_char, nor can it contain any invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
You can specify relative or absolute path information in the path parameter. Relative path information is interpreted as relative to the current working directory, which you can determine by using the xtd::io::directory::get_current_directory method. The returned directory names are prefixed with the value you provided in the path parameter. For example, if you provide a relative path in the path parameter, the returned directory names will contain a relative path.
The xtd::io::directory::enumerate_directories and xtd::io::directory::get_directories methods differ as follows: When you use xtd::io::directory::xtd::io::directory::enumerate_directories, you can start enumerating the collection of names before the whole collection is returned; when you use xtd::io::directory::get_directories, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, xtd::io::directory::enumerate_directories can be more efficient.

◆ enumerate_file_system_entries() [1/2]

static xtd::io::directory::file_system_entry_iterator xtd::io::directory::enumerate_file_system_entries ( const xtd::ustring path)
static

Returns an enumerable collection of file names and directory names in a specified path.

Parameters
pathThe relative or absolute path to the directory to search. This string is not case-sensitive.
Returns
An xtd::io::directory::file_system_entry_iterator of file-system entries in the directory specified by path.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\").
Remarks
You can specify relative path information with the path parameter. Relative path information is interpreted as relative to the current working directory, which you can determine by using the xtd::io::directory::get_current_directory method.
The xtd::io::directory::enumerate_file_system_entries and xtd::io::directory::get_file_system_entries methods differ as follows: When you use xtd::io::directory::enumerate_file_system_entries, you can start enumerating the collection of entries before the whole collection is returned; when you use xtd::io::directory::get_file_system_entries, you must wait for the whole array of entries to be returned before you can access the array. Therefore, when you are working with many files and directories, xtd::io::directory::enumerate_files_system_entires can be more efficient.

◆ enumerate_file_system_entries() [2/2]

static xtd::io::directory::file_system_entry_iterator xtd::io::directory::enumerate_file_system_entries ( const xtd::ustring path,
const xtd::ustring search_pattern 
)
static

Returns an enumerable collection of file names and directory names that match a search pattern in a specified path.

Parameters
pathThe relative or absolute path to the directory to search. This string is not case-sensitive.
serach_patternThe search string to match against the names of file-system entries in path. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.
Returns
An xtd::io::directory::file_system_entry_iterator of file-system entries in the directory specified by path and that match the specified search pattern.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @remarks search_pattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. The following wildcard specifiers are permitted in searchPattern. <table class="markdownTable"> <tr class="markdownTableHead"> <th class="markdownTableHeadNone"> Wildcard specifier

Matches

* (asterisk)

Zero or more characters in that position.

? (question mark)

Zero or one character in that position.

Characters other than the wildcard are literal characters. For example, the search_pattern string "*t" searches for all names in path ending with the letter "t". The search_attern string "s*" searches for all names in path beginning with the letter "s".

Remarks
search_pattern cannot end in two periods ("..") or contain two periods ("..") followed by xtd::io::path::directory_separator_char or xtd::ioo::path::alt_directory_separator_char, nor can it contain any invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
You can specify relative path information with the path parameter. Relative path information is interpreted as relative to the current working directory, which you can determine by using the xtd::io::directory::get_current_directory method.
The xtd::io::directory::enumerate_file_system_entries and xtd::io::directory::get_file_system_entries methods differ as follows: When you use xtd::io::directory::enumerate_file_system_entries, you can start enumerating the collection of entries before the whole collection is returned; when you use xtd::io::directory::get_file_system_entries, you must wait for the whole array of entries to be returned before you can access the array. Therefore, when you are working with many files and directories, xtd::io::directory::enumerate_file_system_entries can be more efficient.

◆ enumerate_files() [1/2]

static xtd::io::directory::file_iterator xtd::io::directory::enumerate_files ( const xtd::ustring path)
static

Returns an enumerable collection of full file names in a specified path.

Parameters
pathThe relative or absolute path to the directory to search. This string is not case-sensitive.
Returns
An xtd::io::directory::directory_iterator of the full names (including paths) for the files in the directory specified by path.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par Examples The following example shows how to retrieve all the text files from a directory and move them to a new directory. After the files are moved, they no longer exist in the original directory. @code #include <xtd/xtd> using namespace xtd; using namespace xtd::io; class program { public: static auto main() { ustring source_directory = R"(C:\current)"; ustring archive_directory = R"(C:\archive)";

try { auto txt_files = directory::enumerate_files(source_directory);

for (ustring current_file : txt_files) { ustring file_name = current_file.substring(source_directory.size() + 1); directory::move(current_file, path::combine(archive_directory, file_name)); } } catch (system_exception& e) { console::write_line(e.message()); } } };

startup_(program::main);

Remarks
You can specify relative path information with the path parameter. Relative path information is interpreted as relative to the current working directory, which you can determine by using the xtd::io::directory::get_current_directory method.
The xtd::io::directory::enumerate_files and xtd::io::directory::get_files methods differ as follows: When you use xtd::io::directory::enumerate_files, you can start enumerating the collection of names before the whole collection is returned. When you use xtd::io::directory::get_files, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, xtd::io::directory::enumerate_files can be more efficient.

◆ enumerate_files() [2/2]

static xtd::io::directory::file_iterator xtd::io::directory::enumerate_files ( const xtd::ustring path,
const xtd::ustring search_pattern 
)
static

Returns an enumerable collection of full file names that match a search pattern in a specified path.

Parameters
pathThe relative or absolute path to the directory to search. This string is not case-sensitive.
search_patternThe search string to match against the names of files in path. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.
Returns
An xtd::io::directory::directory_iterator of the full names (including paths) for the files in the directory specified by path and that match the specified search pattern.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par Examples The following example shows how to retrieve all the text files from a directory and move them to a new directory. After the files are moved, they no longer exist in the original directory. @code #include <xtd/xtd> using namespace xtd; using namespace xtd::io; class program { public: static auto main() { ustring source_directory = R"(C:\current)"; ustring archive_directory = R"(C:\archive)"; try { auto txt_files = directory::enumerate_files(source_directory, "*.txt"); for (ustring current_file : txt_files) { ustring file_name = current_file.substring(source_directory.size() + 1); directory::move(current_file, path::combine(archive_directory, file_name)); } } catch (system_exception& e) { console::write_line(e.message()); } } }; startup_(program::main); @endcode @remarks search_pattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. The following wildcard specifiers are permitted in searchPattern. <table class="markdownTable"> <tr class="markdownTableHead"> <th class="markdownTableHeadNone"> Wildcard specifier

Matches

* (asterisk)

Zero or more characters in that position.

? (question mark)

Zero or one character in that position.

Characters other than the wildcard are literal characters. For example, the search_pattern string "*t" searches for all names in path ending with the letter "t". The search_attern string "s*" searches for all names in path beginning with the letter "s".

Remarks
search_pattern cannot end in two periods ("..") or contain two periods ("..") followed by xtd::io::path::directory_separator_char or xtd::ioo::path::alt_directory_separator_char, nor can it contain any invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
You can specify relative path information with the path parameter. Relative path information is interpreted as relative to the current working directory, which you can determine by using the xtd::io::directory::get_current_directory method.
The xtd::io::directory::enumerate_files and xtd::io::directory::get_files methods differ as follows: When you use xtd::io::directory::enumerate_files, you can start enumerating the collection of names before the whole collection is returned; when you use xtd::io::directory::get_files, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, xtd::io::directory::enumerate_files can be more efficient.

◆ exists()

static bool xtd::io::directory::exists ( const xtd::ustring path)
static

Determines whether the given path refers to an existing directory on disk.

Parameters
pathThe path to test.
Returns
true if path refers to an existing directory; false if the directory does not exist or an error occurs when trying to determine if the specified directory exists.
rExample
The following example takes an array of file or directory names on the command line, determines what kind of name it is, and processes it appropriately.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static auto main(const vector<ustring>& args) {
for (ustring path : args) {
if (file::exists(path)) {
// This path is a file
process_file(path);
} else if(directory::exists(path)) {
// This path is a directory
process_directory(path);
} else {
console::write_line("{0} is not a valid file or directory.", path);
}
}
}
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
static void process_directory(const ustring& target_directory) {
// Process the list of files found in the directory.
vector<ustring> file_entries = directory::get_files(target_directory);
for (ustring file_name : file_entries)
process_file(file_name);
// Recurse into subdirectories of this directory.
vector<ustring> subdirectory_entries = directory::get_directories(target_directory);
for (ustring subdirectory : subdirectory_entries)
process_directory(subdirectory);
}
// Insert logic for processing found files here.
static void process_file(const ustring& path) {
console::write_line("Processed file '{0}'.", path);
}
};
startup_(program::main);
Performs operations on std::basic_string instances that contain file or directory path information....
Definition path.h:34
Remarks
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory.
Trailing spaces are removed from the end of the path parameter before checking whether the directory exists.
The path parameter is not case-sensitive.
If you do not have at a minimum read-only permission to the directory, the Exists method will return false.
The xtd::io::directory::exists method returns false if any error occurs while trying to determine if the specified file exists. This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file.
Examples
test_forms.cpp.

◆ get_creation_time()

static xtd::date_time xtd::io::directory::get_creation_time ( const xtd::ustring path)
static

Gets the creation date and time of a directory.

Parameters
pathThe path of the directory.
Returns
A xtd::date::time class that is set to the creation date and time for the specified directory. This value is expressed in local time.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par Examples @code #include <xtd/xtd> using namespace xtd; using namespace xtd::io; The following example gets the creation time of the specified directory. class program { public: static auto main() { try { // Get the creation time of a well-known directory. date_time dt = directory::get_creation_time(environment::current_directory()); /// // Give feedback to the user. if (date_time::now().subtract(dt).total_days() > 364) { console::write_line("This directory is over a year old."); } else if (date_time::now().subtract(dt).total_days() > 30) { console::write_line("This directory is over a month old."); } else if (date_time::now().subtract(dt).total_days() <= 1) { console::write_line("This directory is less than a day old."); } else { console::write_line("This directory was created on {0}", dt); } } catch (const system_exception& e) { console::write_line("The process failed: {0}", e.to_string()); } } };

startup_(program::main);

◆ get_current_directory()

static xtd::ustring xtd::io::directory::get_current_directory ( )
static

Gets the current working directory of the application.

Returns
A string that contains the absolute path of the current working directory, and does not end with a backslash ().
Exceptions
xtd::unauthorized_access_exceptionThe caller does not have the required permission.
xtd::not_supported_exceptionThe operating system does not have current directory functionality.
Examples
The following example demonstrates how to use the xtd::io::directory::get_current_directory method.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static auto main() {
try {
// Get the current directory.
ustring path = directory::get_current_directory();
ustring target = R"(c:\temp)";
console::write_line("The current directory is {0}", path);
if (!directory::exists(target)) {
directory::create_directory(target);
}
// Change the current directory.
environment::current_directory(target);
if (path.equals(directory::get_current_directory())) {
console::write_line("You are in the temp directory.");
} else {
console::write_line("You are not in the temp directory.");
}
} catch (const system_exception& e) {
console::write_line("The process failed: {0}", e.to_string());
}
}
};
startup_(program::main);
Remarks
The current directory is distinct from the original directory, which is the one from which the process was started.
For a list of common I/O tasks, see Common I/O Tasks.

◆ get_directories() [1/2]

static std::vector< xtd::ustring > xtd::io::directory::get_directories ( const xtd::ustring path)
static

Returns the names of subdirectories (including their paths) in the specified directory.

Parameters
pathThe relative or absolute path to the directory to search. This string is not case-sensitive.
Returns
An array of the full names (including paths) of subdirectories in the specified path, or an empty array if no directories are found.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par Examples The following example takes an array of file or directory names on the command line, determines what kind of name it is, and processes it appropriately. @code #include <xtd/xtd> using namespace std; using namespace xtd; using namespace xtd::io; class program { public: static auto main(const vector<ustring>& args) { for (ustring path : args) { if (file::exists(path)) { // This path is a file process_file(path); } else if(directory::exists(path)) { // This path is a directory process_directory(path); } else { console::write_line("{0} is not a valid file or directory.", path); } } } // Process all files in the directory passed in, recurse on any directories // that are found, and process the files they contain. static void process_directory(const ustring& target_directory) { // Process the list of files found in the directory. vector<ustring> file_entries = directory::get_files(target_directory); for (ustring file_name : file_entries) process_file(file_name); // Recurse into subdirectories of this directory. vector<ustring> subdirectory_entries = directory::get_directories(target_directory); for (ustring subdirectory : subdirectory_entries) process_directory(subdirectory); } // Insert logic for processing found files here. static void process_file(const ustring& path) { console::write_line("Processed file '{0}'.", path); } }; startup_(program::main); @endcode @remarks This method is identical to xtd::io::directory::get_directories(ustring, ustring) with the asterisk (*) specified as the search pattern, so it returns all subdirectories. @remarks The xtd::io::directory::enumerate_directories and xtd::io::directory::get_directories methods differ as follows: When you use xtd::io::directory::enumerate_directories, you can start enumerating the collection of names before the whole collection is returned; when you use xtd::io::directory::get_directories, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, xtd::io::directory::enumerate_directories can be more efficient. @remarks The path parameter can specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see xtd::io::directory::get_current_directory. @remarks The names returned by this method are prefixed with the directory information provided in path. @remarks The path parameter is not case-sensitive. @remarks For a list of common I/O tasks, see <a href="https://gammasoft71.github.io/xtd/docs/documentation/Guides/xtd.core/Common%20I%3AO%20tasks" >Common I/O Tasks.

◆ get_directories() [2/2]

static std::vector< xtd::ustring > xtd::io::directory::get_directories ( const xtd::ustring path,
const xtd::ustring search_pattern 
)
static

Returns the names of subdirectories (including their paths) that match the specified search pattern in the specified directory.

Parameters
pathThe relative or absolute path to the directory to search. This string is not case-sensitive.
search_patternThe search string to match against the names of subdirectories in path. This parameter can contain a combination of valid literal and wildcard characters, but it doesn't support regular expressions.
Returns
An array of the full names (including paths) of the subdirectories that match the search pattern in the specified directory, or an empty array if no directories are found.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par Examples @code #include <xtd/xtd> using namespace std; using namespace xtd; using namespace xtd::io; class program { public: static auto main() { try { // Only get subdirectories that begin with the letter "p." vector<ustring> dirs = directory::get_directories(R"(c:\)", "p*"); console::write_line("The number of directories starting with p is {0}.", dirs.size()); for (ustring dir : dirs) { console::write_line(dir); } } catch (const system_exception& e) { console::write_line("The process failed: {0}", e.to_string()); } } }; startup_(program); @endcode @remarks This method returns all subdirectories directly under the specified directory that match the specified search pattern. If the specified directory has no subdirectories, or no subdirectories match the search_pattern parameter, this method returns an empty array. Only the top directory is searched. @remarks search_pattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. The following wildcard specifiers are permitted in search_pattern. <table class="markdownTable"> <tr class="markdownTableHead"> <th class="markdownTableHeadNone"> Wildcard specifier

Matches

* (asterisk)

Zero or more characters in that position.

? (question mark)

Zero or one character in that position.

Remarks
Characters other than the wildcard are literal characters. For example, the search_attern string "*t" searches for all names in path ending with the letter "t". The search_pattern string "s*" searches for all names in path beginning with the letter "s".
search_pattern cannot end in two periods ("..") or contain two periods ("..") followed by xtd::io::path::directory_separator_char or xtd::io::path::alt_directory_separator_char, nor can it contain any invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
The path parameter can specify relative or absolute path information, and is not case-sensitive. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see xtd::io::directory::get_current_directory.
The xtd::io::directory::enumerate_directories and xtd::io::directory::get_directories methods differ as follows: When you use xtd::io::directory::enumerate_directories, you can start enumerating the collection of names before the whole collection is returned; when you use xtd::io::directory::get_directories, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, xtd::io::directory::enumerate_directories can be more efficient.
For a list of common I/O tasks, see Common I/O Tasks.

◆ get_directory_root()

static xtd::ustring xtd::io::directory::get_directory_root ( const xtd::ustring path)
static

Returns the volume information, root information, or both for the specified path.

Parameters
pathThe path of a file or directory.
Returns
A string that contains the volume information, root information, or both for the specified path.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par Examples The following example illustrates how to set the current directory and display the directory root. @code #include <xtd/xtd> using namespace xtd; using namespace xtd::io; class program { public: static auto main() { // Create string for a directory. This value should be an existing directory // or the sample will throw a DirectoryNotFoundException. ustring dir = R"(C:\test)"; try { //Set the current directory. directory::set_current_directory(dir); } catch (const directory_not_found_exception& e) { console::write_line("The specified directory does not exist. {0}", e); } // Print to console the results. console::write_line("Root directory: {0}", directory::get_directory_root(dir)); console::write_line("Current directory: {0}", directory::get_current_directory()); } }; startup_(program::main); @endcode @remarks This method obtains the fully qualified path name of path, as returned by xtd::io::path::get_full_path, and returns root directory information. The specified path is not required to exist. @remarks The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see xtd::io::directory::get_current_directory. @remarks The path parameter is not case-sensitive. @remarks For a list of common I/O tasks, see <a href="https://gammasoft71.github.io/xtd/docs/documentation/Guides/xtd.core/Common%20I%3AO%20tasks" >Common I/O Tasks.

◆ get_file_system_entries() [1/2]

static std::vector< xtd::ustring > xtd::io::directory::get_file_system_entries ( const xtd::ustring path)
static

Returns the names of all files and subdirectories in a specified path.

Parameters
pathThe relative or absolute path to the directory to search. This string is not case-sensitive.
Returns
An array of the names of files and subdirectories in the specified directory, or an empty array if no files or subdirectories are found.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par Examples The following example uses the xtd::io::directory::get_file_system_entries method to fill an array of strings with the names of all files and subdirectories in a user-specified location and prints each string in the array to the console. The example is configured to catch all errors common to this method. @code #include <xtd/xtd> using namespace std; using namespace xtd; class program { public: static auto main() { program snippets; ustring path = io::directory::get_current_directory(); ustring filter = "*.exe"; snippets.print_file_system_entries(path); snippets.print_file_system_entries(path, filter); snippets.get_logical_drives(); snippets.get_parent(path); snippets.move("C:\proof", "C:\Temp"); } void print_file_system_entries(const ustring& path) { try { // Obtain the file system entries in the directory path. vector<ustring> directory_entries = io::directory::get_file_system_entries(path); for (xtd::ustring str : directory_entries) { console::write_line(str); } } catch (const security::security_exception&) { console::write_line("The caller does not have the required permission."); } catch (const argument_exception&) { console::write_line("path is an empty string, contains only white spaces, or contains invalid characters."); } catch (const io::directory_not_found_exception&) { console::write_line("The path encapsulated in the directory object does not exist."); } } void print_file_system_entries(string path, string pattern) { try { // Obtain the file system entries in the directory path that match the pattern. vector<ustring> directory_entries = io::directory::get_file_system_entries(path, pattern); for (ustring str : directory_entries) { console::write_line(str); } } catch (const security::security_exception&) { console::write_line("The caller does not have the required permission."); } catch (const argument_exception&) { console::write_line("path is an empty string, contains only white spaces, or contains invalid characters."); } catch (const io::directory_not_found_exception&) { console::write_line("The path encapsulated in the directory object does not exist."); } } // Print out all logical drives on the system. void get_logical_drives() { try { vector<ustring> drives = io::directory::get_logical_drives(); for (ustring str : drives) { console::write_line(str); } } catch (const io::io_exception&) { console::write_line("An I/O error occurs."); } catch (const security::security_exception&) { console::write_line("The caller does not have the required permission."); } } void get_parent(const ustring& path) { try { io::directory_info directory_info = io::directory::get_parent(path); console::write_line(directory_info.full_name()); } catch (const argument_exception&) { console::write_line("path is an empty string, contains only white spaces, or contains invalid characters."); } } void move(const ustring& source_path, const ustring& destination_path) { try { io::directory::move(source_path, destination_path); console::write_line("The directory move is complete."); } catch (const security::security_exception&) { console::write_line("The caller does not have the required permission."); } catch (const argument_exception&) { console::write_line("path is an empty string, contains only white spaces, or contains invalid characters."); } catch (const io::io_exception&) { console::write_line("An attempt was made to move a directory to a different volume, or dest_dir_name already exists."); } } }; startup_(program::main); @endcode @remarks The order of the returned file and directory names is not guaranteed; use the std::sort method if a specific sort order is required. @remarks The xtd::io::directory::enumerate_fileSystem_entries and xtd::io::directory::get_file_system_entries methods differ as follows: When you use xtd::io::directory::enumerate_file_system_entries, you can start enumerating the collection of entries before the whole collection is returned; when you use xtd::io::directory::get_file_system_entries, you must wait for the whole array of entries to be returned before you can access the array. Therefore, when you are working with many files and directories, xtd::io::directory::enumerate_file_system_enties can be more efficient. @remarks This method is identical to xtd::io::directory::get_file_system_entries with the asterisk (*) specified as the search pattern. @remarks The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see xtd::io::directory::get_current_directory. @remarks The path parameter is not case-sensitive. @remarks For a list of common I/O tasks, see <a href="https://gammasoft71.github.io/xtd/docs/documentation/Guides/xtd.core/Common%20I%3AO%20tasks" >Common I/O Tasks.

◆ get_file_system_entries() [2/2]

static std::vector< xtd::ustring > xtd::io::directory::get_file_system_entries ( const xtd::ustring path,
const xtd::ustring search_pattern 
)
static

Returns an array of file names and directory names that match a search pattern in a specified path.

Parameters
pathThe relative or absolute path to the directory to search. This string is not case-sensitive.
serach_patternThe search string to match against the names of file and directories in path. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.
Returns
An array of file names and directory names that match the specified search criteria, or an empty array if no files or directories are found.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par Examples The following example uses the xtd::io::directory::get_file_system_entries method to fill an array of strings with the names of all files matching a user-specified filter in a specific location and prints each string in the array to the console. The example is configured to catch all errors common to this method. @code #include <xtd/xtd> using namespace std; using namespace xtd; class program { public: static auto main() { program snippets; ustring path = io::directory::get_current_directory(); ustring filter = "*.exe"; snippets.print_file_system_entries(path); snippets.print_file_system_entries(path, filter); snippets.get_logical_drives(); snippets.get_parent(path); snippets.move("C:\proof", "C:\Temp"); } void print_file_system_entries(const ustring& path) { try { // Obtain the file system entries in the directory path. vector<ustring> directory_entries = io::directory::get_file_system_entries(path); for (xtd::ustring str : directory_entries) { console::write_line(str); } } catch (const security::security_exception&) { console::write_line("The caller does not have the required permission."); } catch (const argument_exception&) { console::write_line("path is an empty string, contains only white spaces, or contains invalid characters."); } catch (const io::directory_not_found_exception&) { console::write_line("The path encapsulated in the directory object does not exist."); } } void print_file_system_entries(string path, string pattern) { try { // Obtain the file system entries in the directory path that match the pattern. vector<ustring> directory_entries = io::directory::get_file_system_entries(path, pattern); for (ustring str : directory_entries) { console::write_line(str); } } catch (const security::security_exception&) { console::write_line("The caller does not have the required permission."); } catch (const argument_exception&) { console::write_line("path is an empty string, contains only white spaces, or contains invalid characters."); } catch (const io::directory_not_found_exception&) { console::write_line("The path encapsulated in the directory object does not exist."); } } // Print out all logical drives on the system. void get_logical_drives() { try { vector<ustring> drives = io::directory::get_logical_drives(); for (ustring str : drives) { console::write_line(str); } } catch (const io::io_exception&) { console::write_line("An I/O error occurs."); } catch (const security::security_exception&) { console::write_line("The caller does not have the required permission."); } } void get_parent(const ustring& path) { try { io::directory_info directory_info = io::directory::get_parent(path); console::write_line(directory_info.full_name()); } catch (const argument_exception&) { console::write_line("path is an empty string, contains only white spaces, or contains invalid characters."); } } void move(const ustring& source_path, const ustring& destination_path) { try { io::directory::move(source_path, destination_path); console::write_line("The directory move is complete."); } catch (const security::security_exception&) { console::write_line("The caller does not have the required permission."); } catch (const argument_exception&) { console::write_line("path is an empty string, contains only white spaces, or contains invalid characters."); } catch (const io::io_exception&) { console::write_line("An attempt was made to move a directory to a different volume, or dest_dir_name already exists."); } } }; startup_(program::main); @endcode @remarks The returned file names are appended to the supplied path parameter and the order of the returned file names is not guaranteed; use the std::sort method if a specific sort order is required. @remarks search_pattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. The following wildcard specifiers are permitted in search_pattern. <table class="markdownTable"> <tr class="markdownTableHead"> <th class="markdownTableHeadNone"> Wildcard specifier

Matches

* (asterisk)

Zero or more characters in that position.

? (question mark)

Zero or one character in that position.

Remarks
Characters other than the wildcard are literal characters. For example, the search_pattern string "*t" searches for all names in path ending with the letter "t". The search_pattern string "s*" searches for all names in path beginning with the letter "s".
search_pattern cannot end in two periods ("..") or contain two periods ("..") followed by xtd::io::path::directory_separator_char or xtd::io::path::alt_directory_separator_char, nor can it contain any invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
Note
When you use the asterisk wildcard character in a search_pattern such as "*.txt", the number of characters in the specified extension affects the search as follows:
  • If the specified extension is exactly three characters long, the method returns files with extensions that begin with the specified extension. For example, "*.xls" returns both "book.xls" and "book.xlsx".
  • In all other cases, the method returns files that exactly match the specified extension. For example, "*.ai" returns "file.ai" but not "file.aif".
    When you use the question mark wildcard character, this method returns only files that match the specified file extension. For example, given two files, "file1.txt" and "file1.txtother", in a directory, a search pattern of "file?.txt" returns just the first file, whereas a search pattern of "file*.txt" returns both files.
Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "*1*.txt" may return unexpected file names. For example, using a search pattern of "*1*.txt" returns "longfilename.txt" because the equivalent 8.3 file name format is "LONGFI~1.TXT".
Remarks
The xtd::io::directory::enumerate_files and xtd::io::directory::get_files methods differ as follows: When you use xtd::io::directory::enumerate_files, you can start enumerating the collection of names before the whole collection is returned; when you use xtd::io::directory::get_files, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, xtd::io::directory::enumerate_files can be more efficient.
The path parameter can specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see xtd::io::directory::get_current_directory.
The path parameter is not case-sensitive.
For a list of common I/O tasks, see Common I/O Tasks.

◆ get_files() [1/2]

static std::vector< xtd::ustring > xtd::io::directory::get_files ( const xtd::ustring path)
static

Returns the names of files (including their paths) in the specified directory.

Parameters
pathThe relative or absolute path to the directory to search. This string is not case-sensitive.
Returns
An array of the full names (including paths) for the files in the specified directory, or an empty array if no files are found.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par Examples The following example demonstrates how to use the GetFiles method to return file names from a user-specified location. The example is configured to catch all errors common to this method. @code #include <xtd/xtd> using namespace std; using namespace xtd; using namespace xtd::io; class program { public: static auto main(const vector<ustring>& args) { for (ustring path : args) { if (file::exists(path)) { // This path is a file process_file(path); } else if(directory::exists(path)) { // This path is a directory process_directory(path); } else { console::write_line("{0} is not a valid file or directory.", path); } } } // Process all files in the directory passed in, recurse on any directories // that are found, and process the files they contain. static void process_directory(const ustring& target_directory) { // Process the list of files found in the directory. vector<ustring> file_entries = directory::get_files(target_directory); for (ustring file_name : file_entries) process_file(file_name); // Recurse into subdirectories of this directory. vector<ustring> subdirectory_entries = directory::get_directories(target_directory); for (ustring subdirectory : subdirectory_entries) process_directory(subdirectory); } // Insert logic for processing found files here. static void process_file(const ustring& path) { console::write_line("Processed file '{0}'.", path); } }; startup_(program::main); @endcode @remarks The xtd::io::directory::enumerate_files and xtd::io::directory::get_files methods differ as follows: When you use xtd::io::directory::enumerate_files, you can start enumerating the collection of names before the whole collection is returned; when you use xtd::io::directory::get_files, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, xtd::io::directory::enumerate_files can be more efficient. @remarks The returned file names are appended to the supplied path parameter. @remarks This method is identical to xtd::io::directory::get_files(ustring, ustring) with the asterisk (*) specified as the search pattern. @remarks The path parameter can specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see xtd::io::directory::get_current_directory. @remarks The order of the returned file names is not guaranteed; use the std:::sort method if a specific sort order is required. @remarks The path parameter is not case-sensitive. @remarks For a list of common I/O tasks, see <a href="https://gammasoft71.github.io/xtd/docs/documentation/Guides/xtd.core/Common%20I%3AO%20tasks" >Common I/O Tasks.

◆ get_files() [2/2]

static std::vector< xtd::ustring > xtd::io::directory::get_files ( const xtd::ustring path,
const xtd::ustring search_pattern 
)
static

Returns the names of files (including their paths) that match the specified search pattern in the specified directory.

Parameters
pathThe relative or absolute path to the directory to search. This string is not case-sensitive.
search_patternThe search string to match against the names of files in path. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.
Returns
An array of the full names (including paths) for the files in the specified directory that match the specified search pattern, or an empty array if no files are found.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par Examples The following example counts the number of files that begin with the specified letter. @code #include <xtd/xtd> using namespace std; using namespace xtd; using namespace xtd::io; class program { public: static auto main() { try { // Only get files that begin with the letter "c". vector<ustring> dirs = directory::get_files(R"(c:", "c*)"); console::write_line("The number of files starting with c is {0}.", dirs.size()); for (ustring dir : dirs) { console::write_line(dir); } } catch (const system_exception& e) { console::write_line("The process failed: {0}", e.to_string()); } } };

startup_(program::main);

Remarks
The returned file names are appended to the supplied path parameter and the order of the returned file names is not guaranteed; use the std::sort method if a specific sort order is required.
search_pattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. The following wildcard specifiers are permitted in search_pattern.
Wildcard specifier Matches
* (asterisk) Zero or more characters in that position.
? (question mark) Zero or one character in that position.
Characters other than the wildcard are literal characters. For example, the search_pattern string "*t" searches for all names in path ending with the letter "t". The search_pattern string "s*" searches for all names in path beginning with the letter "s".
search_pattern cannot end in two periods ("..") or contain two periods ("..") followed by xtd::io::path::directory_separator_char or xtd::io::path::alt_directory_separator_char, nor can it contain any invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
Note
When you use the asterisk wildcard character in a search_pattern such as "*.txt", the number of characters in the specified extension affects the search as follows:
  • If the specified extension is exactly three characters long, the method returns files with extensions that begin with the specified extension. For example, "*.xls" returns both "book.xls" and "book.xlsx".
  • In all other cases, the method returns files that exactly match the specified extension. For example, "*.ai" returns "file.ai" but not "file.aif".
    When you use the question mark wildcard character, this method returns only files that match the specified file extension. For example, given two files, "file1.txt" and "file1.txtother", in a directory, a search pattern of "file?.txt" returns just the first file, whereas a search pattern of "file*.txt" returns both files.
Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "*1*.txt" may return unexpected file names. For example, using a search pattern of "*1*.txt" returns "longfilename.txt" because the equivalent 8.3 file name format is "LONGFI~1.TXT".
Remarks
The xtd::io::directory::enumerate_files and xtd::io::directory::get_files methods differ as follows: When you use xtd::io::directory::enumerate_files, you can start enumerating the collection of names before the whole collection is returned; when you use xtd::io::directory::get_files, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, xtd::io::directory::enumerate_files can be more efficient.
The path parameter can specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see xtd::io::directory::get_current_directory.
The path parameter is not case-sensitive.
For a list of common I/O tasks, see Common I/O Tasks.

◆ get_last_access_time()

static xtd::date_time xtd::io::directory::get_last_access_time ( const xtd::ustring path)
static

Returns the date and time the specified file or directory was last accessed.

Parameters
pathThe file or directory for which to obtain access date and time information.
Returns
A xtd::date_time class that is set to the date and time the specified file or directory was last accessed. This value is expressed in local time.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par Examples The following example demonstrates how to use GetLastAccessTime. @code #include <xtd/xtd> using namespace std::chrono; using namespace xtd; using namespace xtd::io; class program { public: static auto main() { try { ustring path = R"(c:\MyDir)"; if (!directory::exists(path)) { directory::create_directory(path); } directory::set_last_access_time(path, {1985, 5, 4}); // Get the creation time of a well-known directory. date_time dt = directory::get_last_access_time(path); console::write_line("The last access time for this directory was {0}", dt); // Update the last access time. directory::set_last_access_time(path, date_time::now()); dt = directory::get_last_access_time(path); console::write_line("The last access time for this directory was {0}", dt); } catch (const system_exception& e) { console::write_line("The process failed: {0}", e.to_string()); } } }; startup_(program::main); @endcode @note This method may return an inaccurate value, because it uses native functions whose values may not be continuously updated by the operating system. @remarks This method is identical to xtd::io::file::get_last_access_time. @remarks If the directory described in the path parameter does not exist, this method returns 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC), adjusted to local time. @remarks The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see xtd::io::directory::get_current_directory. @remarks The path parameter is not case-sensitive. @remarks For a list of common I/O tasks, see <a href="https://gammasoft71.github.io/xtd/docs/documentation/Guides/xtd.core/Common%20I%3AO%20tasks" >Common I/O Tasks.

◆ get_last_write_time()

static xtd::date_time xtd::io::directory::get_last_write_time ( const xtd::ustring path)
static

Returns the date and time the specified file or directory was last written to.

Parameters
pathThe file or directory for which to obtain modification date and time information.
Returns
A xtd::date_time class that is set to the date and time the specified file or directory was last written to. This value is expressed in local time.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par Examples The following example demonstrates how to use xtd::io::directory::get_last_write_time. @code #include <xtd/xtd> using namespace std::chrono; using namespace xtd; using namespace xtd::io; class program { public: static auto main() { try { ustring path = R"(c:\MyDir)"; if (!directory::exists(path)) { directory::create_directory(path); } directory::set_last_write_time(path, {1985, 5, 4}); // Get the creation time of a well-known directory. date_time dt = directory::get_last_write_time(path); console::write_line("The last write time for this directory was {0}", dt); // Update the last write time. directory::set_last_write_time(path, date_time::now()); dt = directory::get_last_write_time(path); console::write_line("The last write time for this directory was {0}", dt); } catch (const system_exception& e) { console::write_line("The process failed: {0}", e.to_string()); } } }; startup_(program::main); @endcode @note This method may return an inaccurate value, because it uses native functions whose values may not be continuously updated by the operating system. @remarks If the directory described in the path parameter does not exist, this method returns 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC), adjusted to local time. @remarks The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see xtd::io::directory::get_current_directory. @remarks The path parameter is not case-sensitive. @remarks For a list of common I/O tasks, see <a href="https://gammasoft71.github.io/xtd/docs/documentation/Guides/xtd.core/Common%20I%3AO%20tasks" >Common I/O Tasks.

◆ get_logical_drives()

static std::vector< xtd::ustring > xtd::io::directory::get_logical_drives ( )
static

Retrieves the names of the logical drives on this computer in the form "<drive letter>:\".

Returns
The logical drives on this computer.
Exceptions
xtd::io::io_exceptionAn I/O error occurred (for example, a disk error).
xtd::unauthorized_access_exceptionThe caller does not have the required permission.
Examples
The following example uses the xtd::io::directory::get_logical_drives method to assign the name of each drive on the calling computer to an array of strings. Each member of this string array is then printed to the console. The example is configured to catch all errors common to this method.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
class program {
public:
static auto main() {
program snippets;
ustring path = io::directory::get_current_directory();
ustring filter = "*.exe";
snippets.print_file_system_entries(path);
snippets.print_file_system_entries(path, filter);
snippets.get_logical_drives();
snippets.get_parent(path);
snippets.move("C:\\proof", "C:\\Temp");
}
void print_file_system_entries(const ustring& path) {
try {
// Obtain the file system entries in the directory path.
vector<ustring> directory_entries = io::directory::get_file_system_entries(path);
for (xtd::ustring str : directory_entries) {
console::write_line(str);
}
} catch (const security::security_exception&) {
console::write_line("The caller does not have the required permission.");
} catch (const argument_exception&) {
console::write_line("path is an empty string, contains only white spaces, or contains invalid characters.");
console::write_line("The path encapsulated in the directory object does not exist.");
}
}
void print_file_system_entries(string path, string pattern) {
try {
// Obtain the file system entries in the directory path that match the pattern.
vector<ustring> directory_entries = io::directory::get_file_system_entries(path, pattern);
for (ustring str : directory_entries) {
console::write_line(str);
}
} catch (const security::security_exception&) {
console::write_line("The caller does not have the required permission.");
} catch (const argument_exception&) {
console::write_line("path is an empty string, contains only white spaces, or contains invalid characters.");
console::write_line("The path encapsulated in the directory object does not exist.");
}
}
// Print out all logical drives on the system.
void get_logical_drives() {
try {
vector<ustring> drives = io::directory::get_logical_drives();
for (ustring str : drives) {
console::write_line(str);
}
} catch (const io::io_exception&) {
console::write_line("An I/O error occurs.");
} catch (const security::security_exception&) {
console::write_line("The caller does not have the required permission.");
}
}
void get_parent(const ustring& path) {
try {
io::directory_info directory_info = io::directory::get_parent(path);
console::write_line(directory_info.full_name());
} catch (const argument_exception&) {
console::write_line("path is an empty string, contains only white spaces, or contains invalid characters.");
}
}
void move(const ustring& source_path, const ustring& destination_path) {
try {
io::directory::move(source_path, destination_path);
console::write_line("The directory move is complete.");
} catch (const security::security_exception&) {
console::write_line("The caller does not have the required permission.");
} catch (const argument_exception&) {
console::write_line("path is an empty string, contains only white spaces, or contains invalid characters.");
} catch (const io::io_exception&) {
console::write_line("An attempt was made to move a directory to a different volume, or dest_dir_name already exists.");
}
}
};
startup_(program::main);
The exception that is thrown when one of the arguments provided to a method is not valid.
Definition argument_exception.h:21
Exposes instance methods for creating, moving, and enumerating through directories and subdirectories...
Definition directory_info.h:127
The exception that is thrown when part of a file or directory cannot be found.
Definition directory_not_found_exception.h:27
xtd::ustring full_name() const
Gets the full path of the directory or file.
The exception that is thrown when an I/O error occurs.
Definition io_exception.h:27
The exception that is thrown when an I/O error occurs.
Definition security_exception.h:22
Remarks
xtd::io::directory::get_logical_drives returns all of the accessible drives on a particular machine, including the floppy drive and any optical drives.
For a list of common I/O tasks, see Common I/O Tasks.

◆ get_parent()

static xtd::io::directory_info xtd::io::directory::get_parent ( const xtd::ustring path)
static

Retrieves the parent directory of the specified path, including both absolute and relative paths.

Parameters
pathThe path for which to retrieve the parent directory.
Returns
The parent directory, or xtd::io::directory_info::empty if path is the root directory, including the root of a UNC server or share name.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par Examples The following example demonstrates how to use the xtd::io::directory::get_parent method to retrieve the parent directory of a user-specified location, "path". The value returned by the xtd::io::directory::get_parent method is then printed to the console. The example is configured to catch all errors common to this method. @code #include <xtd/xtd> using namespace std; using namespace xtd; class program { public: static auto main() { program snippets; ustring path = io::directory::get_current_directory(); ustring filter = "*.exe"; snippets.print_file_system_entries(path); snippets.print_file_system_entries(path, filter); snippets.get_logical_drives(); snippets.get_parent(path); snippets.move("C:\proof", "C:\Temp"); } void print_file_system_entries(const ustring& path) { try { // Obtain the file system entries in the directory path. vector<ustring> directory_entries = io::directory::get_file_system_entries(path); for (xtd::ustring str : directory_entries) { console::write_line(str); } } catch (const security::security_exception&) { console::write_line("The caller does not have the required permission."); } catch (const argument_exception&) { console::write_line("path is an empty string, contains only white spaces, or contains invalid characters."); } catch (const io::directory_not_found_exception&) { console::write_line("The path encapsulated in the directory object does not exist."); } } void print_file_system_entries(string path, string pattern) { try { // Obtain the file system entries in the directory path that match the pattern. vector<ustring> directory_entries = io::directory::get_file_system_entries(path, pattern); for (ustring str : directory_entries) { console::write_line(str); } } catch (const security::security_exception&) { console::write_line("The caller does not have the required permission."); } catch (const argument_exception&) { console::write_line("path is an empty string, contains only white spaces, or contains invalid characters."); } catch (const io::directory_not_found_exception&) { console::write_line("The path encapsulated in the directory object does not exist."); } } // Print out all logical drives on the system. void get_logical_drives() { try { vector<ustring> drives = io::directory::get_logical_drives(); for (ustring str : drives) { console::write_line(str); } } catch (const io::io_exception&) { console::write_line("An I/O error occurs."); } catch (const security::security_exception&) { console::write_line("The caller does not have the required permission."); } } void get_parent(const ustring& path) { try { io::directory_info directory_info = io::directory::get_parent(path); console::write_line(directory_info.full_name()); } catch (const argument_exception&) { console::write_line("path is an empty string, contains only white spaces, or contains invalid characters."); } } void move(const ustring& source_path, const ustring& destination_path) { try { io::directory::move(source_path, destination_path); console::write_line("The directory move is complete."); } catch (const security::security_exception&) { console::write_line("The caller does not have the required permission."); } catch (const argument_exception&) { console::write_line("path is an empty string, contains only white spaces, or contains invalid characters."); } catch (const io::io_exception&) { console::write_line("An attempt was made to move a directory to a different volume, or dest_dir_name already exists."); } } }; startup_(program::main); @endcode @remarks The path parameter can specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see xtd::io::directory::get_current_directory. @remarks Trailing spaces are removed from the end of the path parameter before getting the directory. @remarks The string returned by this method consists of all characters in the path up to, but not including, the last xtd::io::path::directory_separator_char or alt_directory_separator_char. For example, passing the path "C:\Directory\SubDirectory\test.txt" to xtd::io::directory::get_parent returns "C:\Directory\SubDirectory". Passing "C:\Directory\SubDirectory" returns "C:\Directory". However, passing "C:\Directory\SubDirectory" returns "C:\Directory\SubDirectory", because the ending directory separator is after "SubDirectory".
Remarks
The path parameter is not case-sensitive.
For a list of common I/O tasks, see Common I/O Tasks.

◆ get_permissions()

static xtd::io::file_permissions xtd::io::directory::get_permissions ( const xtd::ustring path)
static

Gets the xtd::io::file_permissions of the directory on the path.

Parameters
pathThe path to the directory.
Returns
The xtd::io::file_permissions of the directory on the path.
Exceptions
xtd::io::directory_not_found_exceptionif directory src does not exists.

◆ move()

static void xtd::io::directory::move ( const xtd::ustring source_dir_name,
const xtd::ustring dest_dir_name 
)
static

Moves a file or a directory and its contents to a new location.

Parameters
source_dir_nameThe path of the file or directory to move.
dest_dir_nameThe path to the new location for source_dir_name. If source_dir_name is a file, then dest_dir_name must also be a file name.
Exceptions
xtd::io::io_exceptionAn attempt was made to move a directory to a different volume.
-or-
dest_dir_name already exists. See the Note in the Remarks section.
-or-
The source_dir_name and dest_dir_name parameters refer to the same file or directory.
-or-
The directory or a file within it is being used by another process.
xtd::argument_exceptionsource_dir_name or dest_dir_name is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters with the xtd::path::io::get_invalid_path_chars() method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe path specified by sourceDirName is invalid (for example, it is on an unmapped drive).
Examples
The following example demonstrates how to move a directory and all its files to a new directory. The original directory no longer exists after it has been moved.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static auto main() {
ustring source_directory = R"(C:\source)";
ustring destination_directory = R"(C:\destination)";
try {
directory::move(source_directory, destination_directory);
} catch (const system_exception& e) {
console::write_line(e.message());
}
}
};
startup_(program::main);
Remarks
This method creates a new directory with the name specified by destDirName and moves the contents of sourceDirName to the newly created destination directory. If you try to move a directory to a directory that already exists, an IOException will occur. For example, an exception will occur if you try to move c:\mydir to c:, and c:already exists. Alternatively, you could specify "c:\\public\\mydir" as the destDirName parameter, provided that "mydir" does not exist under "c:\\public", or specify a new directory name such as "c:\\newdir".
The sourceDirName and destDirName arguments are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.
Trailing spaces are removed from the end of the path parameters before moving the directory.
Note
xtd::io::directory::move method throws an xtd::io::io_exception in all platforms when the dest_dir_name already exists.

◆ remove() [1/2]

static void xtd::io::directory::remove ( const xtd::ustring path)
static

Deletes an empty directory from a specified path.

Parameters
pathThe name of the empty directory to remove. This directory must be writable and empty.
Exceptions
xtd::io::io_exceptionA file with the same name and location specified by path exists.
-or-
The directory is the application's current working directory.
-or-
The directory specified by path is not empty.
-or-
The directory is read-only or contains a read-only file.
-or-
The directory is being used by another process.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par Exaample The following example shows how to create a new directory and subdirectory, and then delete only the subdirectory. @code #include <xtd/xtd> using namespace xtd; using namespace xtd::io; class program { public: static auto main() { ustring sub_path = R"(C:\NewDirectory\NewSubDirectory)"; try { directory::create_directory(sub_path); directory::remove(sub_path); bool directory_exists = directory::exists(R"(C:\NewDirectory)"); bool sub_directory_exists = directory::exists(sub_path); console::write_line("top-level directory exists: {0}", directory_exists); console::write_line("sub-directory exists: {0}", sub_directory_exists); } catch (const system_exception& e) { console::write_line("The process failed: {0}", e.message()); } } };

startup_(program::main);

Remarks
This method behaves identically to Delete(String, Boolean) with false specified for the second parameter.
The path parameter may specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.
Trailing spaces are removed from the end of the path parameter before deleting the directory.
This method throws an IOException if the directory specified in the path parameter contains files or subdirectories.
The path parameter is not case-sensitive.
In some cases, if you have the specified directory open in File Explorer, the Delete method may not be able to delete it.

◆ remove() [2/2]

static void xtd::io::directory::remove ( const xtd::ustring path,
bool  recursive 
)
static

Deletes the specified directory and, if indicated, any subdirectories and files in the directory.

Parameters
pathThe name of the directory to remove.
recursivetrue to remove directories, subdirectories, and files in path; otherwise, false.
Exceptions
xtd::io::io_exceptionA file with the same name and location specified by path exists.
-or-
The directory is the application's current working directory.
-or-
The directory specified by path is not empty.
-or-
The directory is read-only or contains a read-only file.
-or-
The directory is being used by another process.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par Examples The following example shows how to create a new directory, subdirectory, and file in the subdirectory, and then recursively delete all the new items. @code #include <xtd/xtd> using namespace xtd; using namespace xtd::io; class program { public: static auto main() { ustring top_path = R"(C:\NewDirectory)"; ustring sub_path = R"(C:\NewDirectory\NewSubDirectory)"; try { directory::create_directory(sub_path); using_ (stream_writer writer(sub_path + R"(\example.txt)")) { writer.write_line("content added"); } directory::remove(top_path, true); bool directory_exists = directory::exists(top_path); console::write_line("top-level directory exists: {0}", directory_exists); } catch (const system_exception& e) { console::write_line("The process failed: {0}", e.message()); } } };

startup_(program::main);

Remarks
The path parameter may specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see xtd::io::directory::get_current_directory.
Trailing spaces are removed from the end of the path parameter before deleting the directory.
The path parameter is not case-sensitive.
If the recursive parameter is true, the user must have write permission for the current directory as well as for all subdirectories.
The behavior of this method differs slightly when deleting a directory that contains a reparse point, such as a symbolic link or a mount point. If the reparse point is a directory, such as a mount point, it is unmounted and the mount point is deleted. This method does not recurse through the reparse point. If the reparse point is a symbolic link to a file, the reparse point is deleted and not the target of the symbolic link.
In some cases, if you have the specified directory open in File Explorer, the xtd::io::directory::remove method may not be able to delete it.

◆ set_creation_time() [1/2]

static void xtd::io::directory::set_creation_time ( const xtd::ustring path,
const xtd::date_time creation_time 
)
static

Sets the creation date and time for the specified file or directory.

Parameters
pathThe file or directory for which to set the creation date and time information.
creation_timeThe date and time the file or directory was last written to. This value is expressed in local time.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par Examples @code #include <xtd/xtd> using namespace xtd; using namespace xtd::io; class program { public: static auto main() { // Set the directory. ustring n = R"(C:\test\newdir)"; //Create the directory. try { directory::create_directory(n); } catch (const io_exception& e) { console::write_line(e); } //Set the creation and last access times to a variable DateTime value. directory::set_creation_time(n, {2002, 1, 3}); directory::set_last_access_time(n, {2002, 1, 3}); // Print to console the results. console::write_line("Creation Date: {0}", directory::get_creation_time(n)); console::write_line("Last write time: {0}", directory::get_last_write_time(n)); console::write_line("Last access time: {0}", directory::get_last_access_time(n)); //Set the last write time to a different value. directory::set_last_write_time(n, {1999, 1, 1}); console::write_line("Changed last write time: {0}", directory::get_last_write_time(n)); } };

startup_(program::main);

// Obviously, since this sample deals with dates and times, the output will vary // depending on when you run the executable. Here is one example of the output: //Creation Date: 1/3/2002 12:00:00 AM //Last write time: 12/31/1998 4:00:00 PM //Last access time: 1/2/2002 4:00:00 PM //Changed last write time: 1/1/1999 12:00:00 AM

Remarks
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see xtd::io::directory::get_current_directory.
The path parameter is not case-sensitive.

◆ set_creation_time() [2/2]

static void xtd::io::directory::set_creation_time ( const xtd::ustring path,
time_t  creation_time 
)
static

Sets the creation date and time for the specified file or directory.

Parameters
pathThe file or directory for which to set the creation date and time information.
creation_timeThe date and time the file or directory was last written to. This value is expressed in local time.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par Examples @code #include <xtd/xtd> using namespace xtd; using namespace xtd::io; class program { public: static auto main() { // Set the directory. ustring n = R"(C:\test\newdir)"; //Create the directory. try { directory::create_directory(n); } catch (const io_exception& e) { console::write_line(e); } //Set the creation and last access times to a variable DateTime value. directory::set_creation_time(n, {2002, 1, 3}); directory::set_last_access_time(n, {2002, 1, 3}); // Print to console the results. console::write_line("Creation Date: {0}", directory::get_creation_time(n)); console::write_line("Last write time: {0}", directory::get_last_write_time(n)); console::write_line("Last access time: {0}", directory::get_last_access_time(n)); //Set the last write time to a different value. directory::set_last_write_time(n, {1999, 1, 1}); console::write_line("Changed last write time: {0}", directory::get_last_write_time(n)); } };

startup_(program::main);

// Obviously, since this sample deals with dates and times, the output will vary // depending on when you run the executable. Here is one example of the output: //Creation Date: 1/3/2002 12:00:00 AM //Last write time: 12/31/1998 4:00:00 PM //Last access time: 1/2/2002 4:00:00 PM //Changed last write time: 1/1/1999 12:00:00 AM

Remarks
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see xtd::io::directory::get_current_directory.
The path parameter is not case-sensitive.

◆ set_current_directory()

static void xtd::io::directory::set_current_directory ( const xtd::ustring path)
static

Sets the application's current working directory to the specified directory.

Parameters
pathThe path to which the current working directory is set.
Exceptions
xtd::argument_exceptionAttempted to set to an empty string ("").
xtd::io::io_exceptionAn I/O error occurred.
xtd::io::directory_not_found_exceptionAttempted to set a local path that cannot be found.
xtd::security::security_exceptionThe caller does not have the appropriate permission.
Examples
The following example illustrates how to set the current directory and display the directory root.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static auto main() {
// Create string for a directory. This value should be an existing directory
// or the sample will throw a DirectoryNotFoundException.
ustring dir = R"(C:\test)";
try {
//Set the current directory.
directory::set_current_directory(dir);
} catch (const directory_not_found_exception& e) {
console::write_line("The specified directory does not exist. {0}", e);
}
// Print to console the results.
console::write_line("Root directory: {0}", directory::get_directory_root(dir));
console::write_line("Current directory: {0}", directory::get_current_directory());
}
};
startup_(program::main);
Remarks
When the application terminates, the working directory is restored to its original location (the directory where the process was started).
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see xtd::io::directory::get_current_directory.
Trailing spaces are removed from the end of the path parameter before setting the directory.
The path parameter is not case-sensitive.
If you are setting the directory to a drive with removable media (for example, "E:" for a USB flash drive), you can determine whether the drive is ready by using the IsReady property.When the application terminates, the working directory is restored to its original location (the directory where the process was started).
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.
Trailing spaces are removed from the end of the path parameter before setting the directory.
The path parameter is not case-sensitive.
If you are setting the directory to a drive with removable media (for example, "E:" for a USB flash drive), you can determine whether the drive is ready by using the xtd::io::drive::is_ready property.

◆ set_last_access_time()

static void xtd::io::directory::set_last_access_time ( const xtd::ustring path,
const xtd::date_time last_access_time 
)
static

Sets the date and time the specified file or directory was last accessed.

Parameters
pathThe file or directory for which to set the access date and time information.
last_access_timeAn object that contains the value to set for the access date and time of path. This value is expressed in local time.
Exceptions
xtd::argument_exceptionAttempted to set to an empty string ("").
xtd::io::io_exceptionAn I/O error occurred.
xtd::io::directory_not_found_exceptionAttempted to set a local path that cannot be found.
xtd::security::security_exceptionThe caller does not have the appropriate permission.
Examples
The following example demonstrates how to use xtd::directory::set_last_access_time.
#include <xtd/xtd>
using namespace std::chrono;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static auto main() {
try {
ustring path = R"(c:\MyDir)";
if (!directory::exists(path)) {
directory::create_directory(path);
}
directory::set_last_access_time(path, {1985, 5, 4});
// Get the creation time of a well-known directory.
sdate_time dt = directory::get_last_access_time(path);
console::write_line("The last access time for this directory was {0}", dt);
// Update the last access time.
directory::set_last_access_time(path, date_time::now());
dt = directory::get_last_access_time(path);
console::write_line("The last access time for this directory was {0}", dt);
}
catch (const system_exception& e) {
console::write_line("The process failed: {0}", e.to_string());
}
}
};
startup_(program::main);
Remarks
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see xtd::io::directory::get_current_directory.
The path parameter is not case-sensitive.
For a list of common I/O tasks, see Common I/O Tasks.

◆ set_last_write_time()

static void xtd::io::directory::set_last_write_time ( const xtd::ustring path,
const xtd::date_time last_write_time 
)
static

Sets the date and time a directory was last written to.

Parameters
pathThe path of the directory.
last_write_timeThe date and time the directory was last written to. This value is expressed in local time.
Exceptions
xtd::argument_exceptionAttempted to set to an empty string ("").
xtd::io::io_exceptionAn I/O error occurred.
xtd::io::directory_not_found_exceptionAttempted to set a local path that cannot be found.
xtd::security::security_exceptionThe caller does not have the appropriate permission.
Examples
The following example demonstrates how to use xtd::io::set_last_write_time.
#include <xtd/xtd>
using namespace std::chrono;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static auto main() {
try {
ustring path = R"(c:\MyDir)";
if (!directory::exists(path)) {
directory::create_directory(path);
}
directory::set_last_write_time(path, {1985, 5, 4});
// Get the creation time of a well-known directory.
date_time dt = directory::get_last_write_time(path);
console::write_line("The last write time for this directory was {0}", dt);
// Update the last write time.
directory::set_last_write_time(path, date_time::now());
tp = directory::get_last_write_time(path);
console::write_line("The last write time for this directory was {0}", dt);
}
catch (const system_exception& e) {
console::write_line("The process failed: {0}", e.to_string());
}
}
};
startup_(program::main);
Represents an instant in time, typically expressed as a date and time of day.
Definition date_time.h:79
Remarks
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see xtd::io::directory::get_current_directory.
The path parameter is not case-sensitive.
For a list of common I/O tasks, see Common I/O Tasks.

◆ set_permissions()

static void xtd::io::directory::set_permissions ( const xtd::ustring path,
xtd::io::file_permissions  permissions 
)
static

Sets the specified xtd::io::file_permissions of the directory on the specified path.

Parameters
pathThe path to the directory.
attributesA bitwise combination of the enumeration values.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, directory name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @remarks The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see xtd::io::directory::get_current_directory. @remarks For a list of common I/O tasks, see <a href="https://gammasoft71.github.io/xtd/docs/documentation/Guides/xtd.core/Common%20I%3AO%20tasks" >Common I/O Tasks.

The documentation for this class was generated from the following file: