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

◆ remove()

void xtd::io::file_info::remove ( ) const
overridevirtual

Permanently deletes a file.

Exceptions
xtd::io::io_exceptionThe target file is open or memory-mapped on a computer running Microsoft Windows NT.
-or-
There is an open handle on the file, and the operating system is Windows XP or earlier. This open handle can result from enumerating directories and files. For more information, see How to: Enumerate Directories and Files.
xtd::security::security_exceptionThe caller does not have the required permission.
xtd::unauthorized_access_exceptionThe path is a directory.
Examples
The following example demonstrates the xtd::io::file_info::remove method.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static auto main() {
string path = R"(c:\MyTest.txt)";
try {
block_scope_(ofstream ofs = fi1.create_text()) {}
string path2 = path + "temp";
file_info fi2(path2);
//Ensure that the target does not exist.
fi2.remove();
//Copy the file.
fi1.copy_to(path2);
console::write_line("{0} was copied to {1}.", path, path2);
//Delete the newly created file.
fi2.remove();
console::write_line("{0} was successfully deleted.", path2);
} catch (const exception& e) {
console::write_line("The process failed: {0}", e.to_string());
}
}
};
startup_(program::main);
// This code produces output similar to the following;
// results may vary based on the computer/file structure/etc.:
//
//c:\MyTest.txt was copied to c:\MyTest.txttemp.
//c:\MyTest.txttemp was successfully deleted.
Defines the base class for predefined exceptions in the xtd namespace.
Definition exception.h:26
Provides static methods for the creation, copying, deletion, moving, and opening of files,...
Definition file_info.h:41
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
@ 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 creates, closes, and deletes a file.
#include <xtd/xtd>
using namespace xtd::io;
class program {
public:
static auto main() {
// Create a reference to a file.
file_info fi("temp.txt");
// Actually create the file.
std::ofstream ofs = fi.create();
// Modify the file as required, and then close the file.
ofs.close();
// Delete the file.
fi.remove();
}
};
startup_(program::main);
Remarks
If the file does not exist, this method does nothing.

Implements xtd::io::file_system_info.

Examples
file_info.cpp.