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

◆ file_info()

xtd::io::file_info::file_info ( const xtd::string file_name)
explicit

Initializes a new instance of the xtd::io::file_info class, which acts as a wrapper for a file path.

Parameters
file_nameThe fully qualified name of the new file, or the relative file name. Do not end the path with the directory separator character.
Exceptions
xtd:argument_exceptionThe file name is empty, contains only white spaces, or contains invalid characters.
xtd::security::security_exceptionThe caller does not have the required permission.
xtd::unauthorized_access_exceptionAccess to fileName is denied.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::not_supported_exceptionfileName contains a colon (:) in the middle of the string.
Examples
The following example demonstrates some of the main members of the xtd::io::file_info class. When the properties are first retrieved, xtd::io::file_info calls the xtd::io::file_info::refresh method and caches information about the file. On subsequent calls, you must call xtd::io::file_info::refresh to get the latest copy of the information.
#include <xtd/io/file_info>
#include <xtd/io/path>
#include <xtd/block_scope>
#include <xtd/console>
#include <xtd/startup>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static auto main() {
auto path = path::get_temp_file_name();
// Create a file to write to.
block_scope_(auto sw = fi1.create_text()) {
sw.write_line("Hello");
sw.write_line("And");
sw.write_line("Welcome");
}
// Open the file to read from.
block_scope_(auto sr = fi1.open_text()) {
while (!sr.end_of_stream())
console::write_line(sr.read_line());
}
try {
auto path2 = path::get_temp_file_name();
file_info fi2(path2);
// Ensure that the target does not exist.
fi2.remove();
// Copy the file.
fi1.copy_to(path2);
console::write_line("{} was copied to {}.", path, path2);
// Delete the newly created file.
fi2.remove();
console::write_line("{} was successfully deleted.", path2);
} catch (const system_exception& e) {
console::write_line("The process failed: {}", e.to_string());
}
}
};
startup_(program::main);
// This code produces the following output :
// Hello
// And
// Welcome
// /var/folders/d5/k7mxnq214dxf3jbvvvhpbfqh0000gn/T/tmp99dcece9.tmp was copied to /var/folders/d5/k7mxnq214dxf3jbvvvhpbfqh0000gn/T/tmp235be07d.tmp.
// /var/folders/d5/k7mxnq214dxf3jbvvvhpbfqh0000gn/T/tmp235be07d.tmp was successfully deleted.
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
The exception that is thrown when a method call is invalid for the object's current state.
Definition system_exception.h: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.h:175
#define block_scope_(...)
The specified expression is cleared automatically when the scope is ended.
Definition block_scope.h:25
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
You can specify either the fully qualified or the relative file name, but the security check gets the fully qualified name.