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

◆ move_to()

void xtd::io::directory_info::move_to ( const xtd::string dest_dir_name)

Moves a DirectoryInfo instance and its contents to a new path.

Parameters
dest_dir_nameThe name and path to which to move this directory. The destination cannot be another disk volume or a directory with the identical name. It can be an existing directory to which you want to add this directory as a subdirectory.
Exceptions
xtd::io::directory_not_found_exceptionThe specified path is invalid, such as being on an unmapped drive.
xtd::io::io_exceptionThe directory is not empty.
-or-
The directory is the application's current working directory.
-or-
There is an open handle on the directory.
xtd::security::security_exceptionThe caller does not have code access permission to create the directory.
Examples
The following example demonstrates moving a directory.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static auto main() {
// Make a reference to a directory.
directory_info di("TempDir");
// Create the directory only if it does not already exist.
if (di.exists() == false)
di.create();
// Create a subdirectory in the directory just created.
directory_info dis = di.create_subdirectory("SubDir");
// Move the main directory. Note that the contents move with the directory.
if (directory::exists("NewTempDir") == false)
di.move_to("NewTempDir");
try {
// Attempt to delete the subdirectory. Note that because it has been
// moved, an exception is thrown.
dis.remove(true);
} catch (const exception&) {
// Handle this exception in some way, such as with the following code:
// console::write_line("That directory does not exist.");
}
// Point the directory_info reference to the new directory.
//di = directory_info("NewTempDir");
// Delete the directory.
//di.remove(true);
}
};
startup_(program::main);
Defines the base class for predefined exceptions in the xtd namespace.
Definition exception.h:26
void remove() const override
Deletes this xtd::io::directory_info if it is empty.
xtd::io::directory_info create_subdirectory(const xtd::string &path) const
Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to...
void move_to(const xtd::string &dest_dir_name)
Moves a DirectoryInfo instance and its contents to a new path.
Exposes instance methods for creating, moving, and enumerating through directories and subdirectories...
Definition directory_info.h:129
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:175
The xtd::io namespace contains types that allow reading and writing to files and data streams,...
Definition binary_reader.h:16
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Remarks
This method throws an IOException if, for example, you try to move c:\mydir to c:, and c:already exists. You must specify "c:\\public\\mydir" as the destDirName parameter, or specify a new directory name such as "c:\\newdir".
This method permits moving a directory to a read-only directory. The read/write attribute of neither directory is affected.
For a list of common I/O tasks, see Common I/O Tasks.