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

◆ copy_to() [2/2]

xtd::io::file_info xtd::io::file_info::copy_to ( const xtd::string dest_file_name,
bool  overwrite 
) const

Copies an existing file to a new file, allowing the overwriting of an existing file.

Parameters
dest_file_nameThe name of the new file to copy to.
overwritetrue to allow an existing file to be overwritten; otherwise, false.
Returns
A new file, or an overwrite of an existing file if overwrite is true. If the file exists and overwrite is false, an xtd::io::ioo_exception is thrown.
Exceptions
xtd::argument_exceptiondest_file_name is empty, contains only white spaces, or contains invalid characters.
xtd::io_io_exceptionAn error occurs, or the destination file already exists and overwrite is false.
xtd::security::security_exceptionThe caller does not have the required permission.
xtd::io::directory_not_found_exceptionThe directory specified in dest_file_name does not exist.
xtd::unauthorized_access_exceptionAdirectory path is passed in, or the file is being moved to a different drive.
xtd::ioo::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::not_supported_exceptiondest_file_name contains a colon (:) in the middle of the string.
Examples
The following example demonstrates both overloads of the xtd::io::file_info::copy_to method.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static auto main() {
string path = R"(c:\SoureFile.txt)";
string path2 = R"(c:\NewFile.txt)";
file_info fi2(path2);
try {
// Create the source file.
block_scope_(ofstream s(fi1.create())) {
}
//Ensure that the target file does not exist.
if (file::exists(path2)) {
fi2.remove();
}
//Copy the file.f
fi1.copy_to(path2);
console::write_line("{0} was copied to {1}.", path, path2);
} catch (const io_exception& ioex) {
console::write_line(ioex.message());
}
}
};
startup_(program::main);
// This code produces output similar to the following;
// results may vary based on the computer/file structure/etc.:
// Add as many lines as you like...
// Add another line to the output...
virtual const xtd::string & message() const noexcept
Gets message associate to the exception.
Provides static methods for the creation, copying, deletion, moving, and opening of files,...
Definition file_info.h:41
The exception that is thrown when an I/O error occurs.
Definition io_exception.h:29
Performs operations on std::basic_string instances that contain file or directory path information....
Definition path.h:36
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:175
#define block_scope_(...)
The specified expression is cleared automatically when the scope is ended.
Definition block_scope.h:25
@ s
The S 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 copying one file to another file, throwing an exception if the destination file already exists.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static auto main() {
// Create a reference to a file, which might or might not exist.
// If it does not exist, it is not yet created.
file_info fi("temp.txt");
// Create a writer, ready to add entries to the file.
std::ofstream ofs(fi.append_text());
stream_writer sw(ofs);
sw.write_line("Add as many lines as you like...");
sw.write_line("Add another line to the output...");
sw.flush();
sw.close();
// Get the information out of the file and display it.
std::ifstream ifs(fi.open_read());
stream_reader sr(ifs);
console::write_line("This is the information in the first file:");
while (sr.peek() != -1)
console::write_line(sr.read_line());
// Copy this file to another file. The true parameter specifies that the file will be overwritten if it already exists.
file_info newfi = fi.copy_to("newTemp.txt", true);
// Get the information out of the new file and display it.
ifs = newfi.open_read();
sr = stream_reader(ifs);
console::write_line("{0}This is the information in the second file:", environment::new_line());
while (sr.peek() != -1)
console::write_line(sr.read_line());
}
};
startup_(program::main);
// This code produces output similar to the following;
// results may vary based on the computer/file structure/etc.:
//
// This is the information in the first file:
// Add as many lines as you like...
// Add another line to the output...
// Add as many lines as you like...
// Add another line to the output...
// This is the information in the second file:
// Add as many lines as you like...
// Add another line to the output...
// Add as many lines as you like...
// Add another line to the output...
xtd::io::file_info copy_to(const xtd::string &dest_file_name) const
Copies an existing file to a new file, disallowing the overwriting of an existing file.
std::ifstream open_read() const
Creates a read-only std::ifstream.
Implements a xtd::io::text_reader that reads characters from a byte stream.
Definition stream_reader.h:28
Implements a xtd::io::text_writer for writing characters to a stream.
Definition stream_writer.h:28
Remarks
Use this method to allow or prevent overwriting of an existing file. Use the xtd::io::file_info::copy_to(string) method to prevent overwriting of an existing file by default.
Warning
Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method. If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable behavior