#include <xtd/io/file_info>
#include <xtd/io/directory_info>
#include <xtd/io/path>
#include <xtd/console>
#include <xtd/environment>
#include <xtd/startup>
class program {
private:
inline static string source_path = path::combine(environment::get_folder_path(environment::special_folder::my_documents), "file_info_test_directory", "move_from", "from_file.xml");
inline static string dest_path = path::combine(environment::get_folder_path(environment::special_folder::my_documents), "file_info_test_directory", "dest_filexml");
public:
static auto main() {
console::clear();
console::set_window_position(0, 0);
console::set_window_size(console::largest_window_width() - 24, console::largest_window_height() - 16);
console::write_line("Welcome.");
console::write_line("This application demonstrates the file_info::move_to method.");
console::write_line("Press any key to start.");
console::read_key();
console::write(" Checking whether ");
console::write(source_path);
console::write_line(" exists.");
ensure_source_file_exists();
display_file_properties(f_info);
console::write_line("Preparing to move the file to ");
console::write(dest_path);
console::write_line(".");
move_file(f_info);
display_file_properties(f_info);
console::write_line("Preparing to delete directories.");
delete_files();
console::write_line("Press the ENTER key to close this application.");
console::read_key();
}
private:
try {
console::write_line("File moved to ");
console::write_line(dest_path);
display_exception(ex);
}
}
static void ensure_source_file_exists() {
if (!directory::exists(dir_path))
directory::create_directory(dir_path);
if (file::exists(dest_path))
file::remove(dest_path);
console::write("Creating file ");
console::write_line(".");
try {
console::write_line("Adding data to the file.");
write_file_content(10);
console::write_line("Successfully created the file.");
}
display_exception(ex);
}
}
static void write_file_content(int total_element) {
lines.
push_back(
"<?xml version=\"1.0\" standalone=\"yes\"?>");
for (auto index = 0; index < total_element; ++index)
lines.push_back(string::format("<MyElement Index=\"{0}\">\nMyElement at position {0}.", index));
file::write_all_lines(source_path, lines);
}
static void display_file_properties(
const file_info& f_info) {
console::write_line("The file_info instance shows these property values.");
try {
console::write("Full_name: ");
console::write("creation_time: ");
console::write("last_write_time: ");
console::write_line();
console::write_line("file contents:");
console::write_line();
console::write_line();
display_exception(ex);
}
}
static void delete_files() {
try {
auto d_info =
directory_info {path::combine(environment::get_folder_path(environment::special_folder::my_documents),
"file_infoTestDirectory")};
if (d_info.exists()) {
console::write_line("Successfully deleted directories and files.");
}
display_exception(ex);
}
}
auto s = string::empty_string;
s += "An exception of type \"";
s += "\" has occurred.\r\n";
s += "\r\nStack trace information:\r\n";
console::write_line(s);
}
};
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:71
virtual void push_back(const type_t &value)
Appends the given element value to the end of the container.
Definition list.hpp:778
virtual const xtd::string & message() const noexcept
Gets message associate to the exception.
virtual xtd::string stack_trace() const noexcept
Gets a string representation of the immediate frames on the call stack.
Exposes instance methods for creating, moving, and enumerating through directories and subdirectories...
Definition directory_info.hpp:129
void remove() const override
Deletes this xtd::io::directory_info if it is empty.
Provides static methods for the creation, copying, deletion, moving, and opening of files,...
Definition file_info.hpp:41
bool exists() const override
Gets a value indicating whether a file exists.
xtd::io::directory_info directory() const
Gets an instance of the parent directory.
void move_to(const xtd::string &dest_file_name)
Moves a specified file to a new location, providing the option to specify a new file name.
const xtd::date_time & creation_time() const
Gets the creation time of the current file or directory.
xtd::string full_name() const
Gets the full path of the directory or file.
const xtd::date_time & last_write_time() const
Gets the time when the current file or directory was last written to.
Implements a xtd::io::text_reader that reads characters from a byte stream.
Definition stream_reader.hpp:28
bool end_of_stream() const
Gets a value that indicates whether the current stream position is at the end of the stream.
virtual xtd::string read_line()
Reads a line of characters from the current stream and returns the data as a string.
The exception that is thrown when a method call is invalid for the object's current state.
Definition system_exception.hpp:18
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:175
#define typeof_
Used to obtain the type object of a specified type or object.
Definition typeof.hpp:45
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:15
The xtd::io namespace contains types that allow reading and writing to files and data streams,...
Definition binary_reader.hpp:16
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10