xtd - Reference Guide  0.2.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
Static Public Attributes | Public Member Functions | List of all members
xtd::io::file_info Class Reference

#include <file_info.h>

Definition

Provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of std::fstream objects.

Namespace
xtd::io
Library
xtd.core
Example
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/xtd>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
file_info fi1(path);
// Create a file to write to.
using_(auto stream = fi1.create_text()) {
stream_writer sw(stream);
sw.write_line("Hello");
sw.write_line("And");
sw.write_line("Welcome");
}
// Open the file to read from.
using_(auto stream = fi1.open_text()) {
stream_reader sr(stream);
while (!sr.end_of_stream())
console::write_line(sr.read_line());
}
try {
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);
// 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.
Remarks
Use the xtd::io::file_info class for typical operations such as copying, moving, renaming, creating, opening, deleting, and appending to files.
If you are performing multiple operations on the same file, it can be more efficient to use td::io::file_info instance methods instead of the corresponding static methods of thetd::io::file class, because a security check will not always be necessary.
Many of the xtd::io::file_info methods return other I/O types when you create or open files. You can use these other types to further manipulate a file. For more information, see specific td::io::file_info members such as td::io::file_info::open, td::io::file_info::open_read, td::io::file_info::open_text, td::io::file_info::create_text, or td::io::file_info::create.
By default, full read/write access to new files is granted to all users.

Static Public Attributes

static const file_info empty
 Represents the uninitialized xtd::io::file_info object. This field is constant. More...
 

Public Member Functions

 file_info (const xtd::ustring &file_name)
 Initializes a new instance of the xtd::io::file_info class, which acts as a wrapper for a file path. More...
 
std::ofstream create () const
 Creates a std::ofstream that appends text to the file represented by this instance of the xtd::io::file_info. More...
 
std::ofstream create_text () const
 Creates a std::ofstream that writes a new text file. More...
 
xtd::io::directory_info directory () const
 Gets an instance of the parent directory. More...
 
xtd::ustring directory_name () const
 Gets a string representing the directory's full path. More...
 
bool exists () const override
 Gets a value indicating whether a file exists. More...
 
bool is_read_only () const
 Gets a value that determines if the current file is read only. More...
 
void is_read_only (bool value)
 Sets a value that determines if the current file is read only. More...
 
size_t length () const
 Gets the size, in bytes, of the current file. More...
 
void move_to (const xtd::ustring &dest_file_name)
 Moves a specified file to a new location, providing the option to specify a new file name. More...
 
void move_to (const xtd::ustring &dest_file_name, bool overwrite)
 
xtd::ustring name () const override
 Gets the name of the file. More...
 
std::fstream open (std::ios::openmode mode) const
 
std::ifstream open_read () const
 
std::ifstream open_text () const
 
std::ofstream open_write () const
 
void remove () const override
 Permanently deletes a file. More...
 
void replace (const xtd::ustring &destination_file_name, const xtd::ustring &destination_backup_file_name)
 
- Public Member Functions inherited from xtd::io::file_system_info
xtd::io::file_attributes attributes () const
 Gets the attributes for the current file or directory. More...
 
xtd::io::file_system_infoattributes (xtd::io::file_attributes value)
 Sets the attributes for the current file or directory. More...
 
std::chrono::system_clock::time_point creation_time () const
 Gets the creation time of the current file or directory. More...
 
xtd::io::file_system_infocreation_time (std::chrono::system_clock::time_point value)
 Gets the creation time of the current file or directory. More...
 
virtual xtd::ustring extension () const
 Gets the extension part of the file name, including the leading dot . even if it is the entire file name, or an empty string if no extension is present. More...
 
xtd::ustring full_name () const
 Gets the full path of the directory or file. More...
 
std::chrono::system_clock::time_point last_access_time () const
 Gets the time the current file or directory was last accessed. More...
 
xtd::io::file_system_infolast_access_time (std::chrono::system_clock::time_point value)
 Sets the time the current file or directory was last accessed. More...
 
std::chrono::system_clock::time_point last_write_time () const
 Gets the time when the current file or directory was last written to. More...
 
xtd::io::file_system_infolast_write_time (std::chrono::system_clock::time_point value)
 Sets the time when the current file or directory was last written to. More...
 
void refresh ()
 Refreshes the state of the object. More...
 
xtd::ustring to_string () const noexcept override
 Returns the original path. Use the xtd::io::file_system_info::full_name or xtd::io::file_system_info::name properties for the full path or file/directory name. More...
 

Additional Inherited Members

- Protected Attributes inherited from xtd::io::file_system_info
xtd::ustring full_path_
 Represents the fully qualified path of the directory or file. More...
 
xtd::ustring original_path_
 The path originally specified by the user, whether relative or absolute. More...
 
- Protected Member Functions inherited from xtd::io::file_system_info
 file_system_info ()=default
 Initializes a new instance of the xtd::io::file_system_info class. More...
 

Constructor & Destructor Documentation

◆ file_info()

xtd::io::file_info::file_info ( const xtd::ustring file_name)

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.
Example
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/xtd>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
file_info fi1(path);
// Create a file to write to.
using_(auto stream = fi1.create_text()) {
stream_writer sw(stream);
sw.write_line("Hello");
sw.write_line("And");
sw.write_line("Welcome");
}
// Open the file to read from.
using_(auto stream = fi1.open_text()) {
stream_reader sr(stream);
while (!sr.end_of_stream())
console::write_line(sr.read_line());
}
try {
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);
// 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.
Remarks
You can specify either the fully qualified or the relative file name, but the security check gets the fully qualified name.

Member Function Documentation

◆ create()

std::ofstream xtd::io::file_info::create ( ) const

Creates a std::ofstream that appends text to the file represented by this instance of the xtd::io::file_info.

Returns
a new A new std::ofstream. Examplle
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
file_info fi(R"(c:\MyTest.txt)");
// This text is added only once to the file.
if (!fi.exists()) {
//Create a file to write to.
using_ (ofstream s(fi.create_text())) {
sw.write_line("Hello");
sw.write_line("And");
sw.write_line("Welcome");
}
}
// This text will always be added, making the file longer over time
// if it is not deleted.
using_ (ofstream s = fi.append_text()) {
sw.write_line("This");
sw.write_line("is Extra");
sw.write_line("Text");
}
//Open the file to read from.
using_ (ifstream s = fi.open_text()) {
while (!sr.end_of_stream()) {
console::write_line(sr.read_line());
}
}
}
};
startup_(program);
// This code produces output similar to the following;
// results may vary based on the computer/file structure/etc.:
//
// Hello
// And
// This
// is Extra
// Text
//When you run this application a second time, you will see the following output:
//
// Hello
// And
// Welcome
// This
// is Extra
// This
// is Extra
// Text
Example
The following example demonstrates appending text to the end of a file and also displays the result of the append operation to the console. The first time this routine is called, the file is created if it does not exist. After that, the specified text is appended to the file.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
static void main() {
file_info fi("temp.txt");
// Create a writer, ready to add entries to the file.
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.
// Remember that the file might have other lines if it already existed.
ifstream ifs(fi.open_read());
stream_reader sr(ifs);
while (sr.peek() != -1)
console::write_line(sr.read_line());
}
};
startup_(program);
// 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...
@encoode
std::ofstream append_text() const;
@brief Copies an existing file to a new file, disallowing the overwriting of an existing file.
@param desy_file_name The name of the new file to copy to.
@return A new file with a fully qualified path.
@exception xtd::argument_exception dest_file_name is empty, contains only white spaces, or contains invalid characters.
@exception xtd::io_io_exception An error occurs, or the destination file already exists and overwrite is false.
@exception xtd::security::security_exception The caller does not have the required permission.
@exception xtd::io::directory_not_found_exception The directory specified in dest_file_name does not exist.
@exception xtd::unauthorized_access_exceptionA directory path is passed in, or the file is being moved to a different drive.
@exception xtd::ioo::path_too_long_exception The specified path, file name, or both exceed the system-defined maximum length.
@exception xttd::not_supported_exception dest_file_name contains a colon (:) in the middle of the string.
@par Example
The following example demonstrates both overloads of the td::io::file_info::copy_to method.
@code
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
ustring path = R"(c:\SoureFile.txt)";
ustring path2 = R"(c:\NewFile.txt)";
file_info fi1(path);
file_info fi2(path2);
try {
// Create the source file.
using_(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) {
}
}
};
startup_(program);
// 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...
Example
The following example demonstrates copying one file to another file, throwing an exception if the destination file already exists.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
try {
// 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.
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.
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 file will not be overwritten if it already exists.
file_info newfi = fi.copy_to("newTemp.txt");
// 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());
} catch(const system_exception& e) {
}
}
};
startup_(program);
// 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...
// This is the information in the second file:
// Add as many lines as you like...
// Add another line to the output...
@encode
@remarks Use the xtd::io::file_info::copy_to(ustring, bool) method to allow overwriting of an existing file.
@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
xtd::io::file_info copy_to(const xtd::ustring& dest_file_name) const;
@brief Copies an existing file to a new file, allowing the overwriting of an existing file.
@param dest_file_name The name of the new file to copy to.
@param overwrite true to allow an existing file to be overwritten; otherwise, false.
@return 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.
@exception xtd::argument_exception dest_file_name is empty, contains only white spaces, or contains invalid characters.
@exception xtd::io_io_exception An error occurs, or the destination file already exists and overwrite is false.
@exception xtd::security::security_exception The caller does not have the required permission.
@exception xtd::io::directory_not_found_exception The directory specified in dest_file_name does not exist.
@exception xtd::unauthorized_access_exceptionA directory path is passed in, or the file is being moved to a different drive.
@exception xtd::ioo::path_too_long_exception The specified path, file name, or both exceed the system-defined maximum length.
@exception xttd::not_supported_exception dest_file_name contains a colon (:) in the middle of the string.
@par Example
The following example demonstrates both overloads of the td::io::file_info::copy_to method.
@code
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
ustring path = R"(c:\SoureFile.txt)";
ustring path2 = R"(c:\NewFile.txt)";
file_info fi1(path);
file_info fi2(path2);
try {
// Create the source file.
using_(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) {
}
}
};
startup_(program);
// 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...
Example
The following example demonstrates copying one file to another file, throwing an exception if the destination file already exists.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void 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.
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.
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);
// 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...
@encode
@remarks Use this method to allow or prevent overwriting of an existing file. Use the xtd::io::file_info::copy_to(ustring) 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
xtd::io::file_info copy_to(const xtd::ustring& dest_file_name, bool overwrite) const;
@brief Creates a file.
@return A new file.
@par Example
The following example creates a reference to a file, and then creates the file on disk using xtd::io::file_info.create().
@code
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
// Create a reference to a file.
file_info fi("temp.txt");
// Actually create the file.
ofstream ofs( fi.create());
// Modify the file as required, and then close the file.
ofs.close();
// Delete the file.
fi.remove();
}
};
startup_(program);
Example
The following example creates a file, adds some text to it, and reads from the file.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
ustring path = R"(c:\MyTest.txt)";
file_info fi(path);
// Delete the file if it exists.
if (fi.exists())
fi.remove();
//Create the file.
using_ (ofstream ofs(fi.create())){
ustring s = "This is some text in the file.";
vector<char> info(s.begin(), s.end());
//Add some information to the file.
ofs.write(&info[0], info.size());
}
//Open the stream and read it back.
using_(ifstream ifs(fi.open_text())) {
stream_reader sr(ifs);
while (!sr.end_of_stream()) {
console::write_line(sr.read_line());
}
}
}
};
startup_(program);
// This code produces output similar to the following;
// results may vary based on the computer/file structure/etc.:
//
// This is some text in the file.
Remarks
By default, full read/write access to new files is granted to all users.
This method is a wrapper for the functionality provided by xtd::io::file::create.

◆ create_text()

std::ofstream xtd::io::file_info::create_text ( ) const

Creates a std::ofstream that writes a new text file.

Returns
A new std::ofstream.
Exceptions
xtd::unauthorized_access_exceptionThe file name is a directory.
xtd::io::io_exceptionThe disk is read-only.
xtd::security::security_exceptionThe caller does not have the required permission.
Example
The following example demonstrates the xtd::io::file┬░info::create_text method.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
ustring path = R"(c:\temp\MyTest.txt)";
file_info fi(path);
if (!fi.exists()) {
//Create a file to write to.
using_(ofstream ofs(fi.create_text())) {
stream_writer sw(ofs);
sw.write_line("Hello");
sw.write_line("And");
sw.write_line("Welcome");
}
}
//Open the file to read from.
using_(ifstream ifs(fi.open_text())) {
stream_reader sr(ifs);
while (!sr.end_of_stream()) {
console::write_line(sr.read_line());
}
}
}
};
startup_(program);
// This code produces output similar to the following;
// results may vary based on the computer/file structure/etc.:
//
// Hello
// And
// Welcome
Remarks
By default, full read/write access to new files is granted to all users.

◆ directory()

xtd::io::directory_info xtd::io::file_info::directory ( ) const

Gets an instance of the parent directory.

Returns
A xtd::io::directory_info object representing the parent directory of this file.
Exceptions
xtd::io::directory_not_found_exceptionThe specified path is invalid, such as being on an unmapped drive.
xtd::security::security_exceptionThe caller does not have the required permission.

◆ directory_name()

xtd::ustring xtd::io::file_info::directory_name ( ) const

Gets a string representing the directory's full path.

Returns
A string representing the directory's full path.
Exceptions
xtd::security::security_exceptionThe caller does not have the required permission.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
Exemple
The following example retrieves the full path of the specified file.
ustring file_name = R"(C:\TMP\log.txt)";
file_info file_info(file_name);
if (!file_info.exists())
return;
console::write_line("{0} has a directoryName of {1}", file_name, file_info.directory_name());
/* This code produces output similar to the following,
* though actual results may vary by machine:
*
* C:\TMP\log.txt has a directory name of C:\TMP
*/
Remarks
To get the parent directory as a xtd::io::directory_info object, use the xtd::io::file_info::directory property.
When first called, xtd::io::file_info calls xtd::io::file_info::refresh 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.

◆ exists()

bool xtd::io::file_info::exists ( ) const
overridevirtual

Gets a value indicating whether a file exists.

Returns
true if the file exists; false if the file does not exist or if the file is a directory.
Example
The following code example uses the xtd::io::file_info::exists property ensure a file exists before opening it. You can use this technique to throw a custom exception when the file is not found.
std::vector<char> open_data_file(const ustring& file_name) {
// Check the FileName argument.
if (file_name.size() == 0)
throw argument_exception("file_name", csf_);
// Check to see if the file exists.
file_info f_info(file_name);
// You can throw a personalized exception if
// the file does not exist.
if (!f_info.exists())
throw file_not_found_exception("The file was not found.", csf_);
// Open the file.
std::ifstream f_stream(file_name, std::ios::binary);
// Create a buffer.
std::vector<char> buffer(f_info.size());
// Read the file contents to the buffer.
f_stream.read(&buffer[0], f_info.size());
// return the buffer.
return buffer;
}
Remarks
When first called, xtd::io::file_info calls xtd::io::file_info::refresh 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.
The xtd::io::file_info::exists property returns false if any error occurs while trying to determine if the specified file exists. This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file.

Implements xtd::io::file_system_info.

◆ is_read_only() [1/2]

bool xtd::io::file_info::is_read_only ( ) const

Gets a value that determines if the current file is read only.

Returns
true if the current file is read only; otherwise, false.
Exceptions
xtd::io::file_not_found_exceptionThe file described by the current FileInfo object could not be found.
xtd::io::io_exceptionAn I/O error occurred while opening the file.
xtd::unauthorized_access_exceptionThis operation is not supported on the current platform. -or- The caller does not have the required permission.
xtd::argument_exceptionThe user does not have write permission, but attempted to set this property to false.
Example
The following example uses the xtd::io::file_info::is_read_only property to mark a file as read only and then mark it as read-write.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
ustring file_name = R"(c:\test.xml)";
// Get the read-only value for a file.
bool is_read_only = is_file_read_only(file_name);
// Display wether the file is read-only.
console::write_line("The file read-only value for " + file_name + " is: " + is_read_only);
console::write_line("Changing the read-only value for " + file_name + " to true.");
// Set the file to read-only.
set_file_read_access(file_name, true);
// Get the read-only value for a file.
is_read_only = is_file_read_only(file_name);
// Display that the file is read-only.
console::write_line("The file read-only value for " + file_name + " is: " + is_read_only);
}
// Sets the read-only value of a file.
static void set_file_read_access(const ustring& file_name, bool set_read_only) {
// Create a new FileInfo object.
file_info f_info(file_name);
// Set the IsReadOnly property.
f_info.is_read_only(set_read_only);
}
// Returns wether a file is read-only.
static bool is_file_read_only(const ustring& file_name) {
// Create a new FileInfo object.
file_info f_info(file_name);
// Return the IsReadOnly property value.
return f_info.is_read_only();
}
};
startup_(program);
// This code produces output similar to the following;
// results may vary based on the computer/file structure/etc.:
//
// The file read-only value for c:\test.xml is: True
// Changing the read-only value for c:\test.xml to true.
// The file read-only value for c:\test.xml is: True
Remarks
Use the xtd::io::file_info::is_read_only property to quickly determine or change whether the current file is read only.
When first called, file_info calls xtd::io::file_info::refresh 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.

◆ is_read_only() [2/2]

void xtd::io::file_info::is_read_only ( bool  value)

Sets a value that determines if the current file is read only.

Parameters
valuetrue if the current file is read only; otherwise, false.
Exceptions
xtd::io::file_not_found_exceptionThe file described by the current FileInfo object could not be found.
xtd::io::io_exceptionAn I/O error occurred while opening the file.
xtd::unauthorized_access_exceptionThis operation is not supported on the current platform. -or- The caller does not have the required permission.
xtd::argument_exceptionThe user does not have write permission, but attempted to set this property to false.
Example
The following example uses the xtd::io::file_info::is_read_only property to mark a file as read only and then mark it as read-write.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
ustring file_name = R"(c:\test.xml)";
// Get the read-only value for a file.
bool is_read_only = is_file_read_only(file_name);
// Display wether the file is read-only.
console::write_line("The file read-only value for " + file_name + " is: " + is_read_only);
console::write_line("Changing the read-only value for " + file_name + " to true.");
// Set the file to read-only.
set_file_read_access(file_name, true);
// Get the read-only value for a file.
is_read_only = is_file_read_only(file_name);
// Display that the file is read-only.
console::write_line("The file read-only value for " + file_name + " is: " + is_read_only);
}
// Sets the read-only value of a file.
static void set_file_read_access(const ustring& file_name, bool set_read_only) {
// Create a new FileInfo object.
file_info f_info(file_name);
// Set the IsReadOnly property.
f_info.is_read_only(set_read_only);
}
// Returns wether a file is read-only.
static bool is_file_read_only(const ustring& file_name) {
// Create a new FileInfo object.
file_info f_info(file_name);
// Return the IsReadOnly property value.
return f_info.is_read_only();
}
};
startup_(program);
// This code produces output similar to the following;
// results may vary based on the computer/file structure/etc.:
//
// The file read-only value for c:\test.xml is: True
// Changing the read-only value for c:\test.xml to true.
// The file read-only value for c:\test.xml is: True
Remarks
Use the xtd::io::file_info::is_read_only property to quickly determine or change whether the current file is read only.
When first called, file_info calls xtd::io::file_info::refresh 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.

◆ length()

size_t xtd::io::file_info::length ( ) const

Gets the size, in bytes, of the current file.

Returns
The size of the current file in bytes.
Exceptions
xtd::io::io_exceptionxtd::io::file_info::refresh cannot update the state of the file or directory.
xtd::io::file_not_found_exceptionThe file does not exist. -or- The xtd::io::file_info::size property is called for a directory.
Example
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
int main() {
// Make a reference to a directory.
directory_info di("c:\\");
// Get a reference to each file in that directory.
vector<file_info> fi_arr = di.get_files();
// Display the names and sizes of the files.
console::write_line("The directory {0} contains the following files:", di.name());
for (file_info f : fi_arr)
console::write_line("The size of {0} is {1} bytes.", f.name(), f.length());
}
// This code produces the following to the following;
// results may vary based on the computer/file structure/etc.:
//
// The directory c:\ contains the following files:
// The size of MyComputer.log is 274 bytes.
// The size of AUTOEXEC.BAT is 0 bytes.
// The size of boot.ini is 211 bytes.
// The size of CONFIG.SYS is 0 bytes.
// The size of hiberfil.sys is 1072775168 bytes.
// The size of IO.SYS is 0 bytes.
// The size of MASK.txt is 2700 bytes.
// The size of mfc80.dll is 1093632 bytes.
// The size of mfc80u.dll is 1079808 bytes.
// The size of MSDOS.SYS is 0 bytes.
// The size of NTDETECT.COM is 47564 bytes.
// The size of ntldr is 250032 bytes.
// The size of pagefile.sys is 1610612736 bytes.
// The size of UpdatePatch.log is 22778 bytes.
// The size of UpdatePatch.txt is 30 bytes.
// The size of wt3d.ini is 234 bytes.
//
Remarks
The value of the Length property is pre-cached if the current instance of the FileInfo object was returned from any of the following DirectoryInfo methods:
  • xtd::io::file_info::get_directories
  • xtd::io::file_info::_det_files
  • xtd::io::file_info::get_file_system_infos
  • xtd::io::file_info::enumerate_directories
  • xtd::io::file_info::enumerate_files
  • xtd::io::file_info::enumerate_file_system_infos
  • To get the latest value, call the xtd::io::file_info::refresh method.

◆ move_to()

void xtd::io::file_info::move_to ( const xtd::ustring dest_file_name)

Moves a specified file to a new location, providing the option to specify a new file name.

Parameters
dest_file_nameThe path to move the file to, which can specify a different file name.
Exceptions
xtd::io::io_exceptionAn I/O error occurs, such as the destination file already exists or the destination device is not ready.
xtd::argument_exceptiondest_file_name is empty, contains only white spaces, or contains invalid characters.
xttd::security::security_exceptionThe caller does not have the required permission.
UnauthorizedAccessExceptiondestFileName is read-only or is a directory.
xtd::io::file_not_found_exceptionThe file is not found.
xtd::io::directory_not_found_exceptionThe specified path is invalid, such as being on an unmapped drive.
xtd::io::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.
Example
Remarks
This method works across disk volumes. For example, the file c:.txt can be moved to d:and renamed NewFile.txt.
This method does not overwrite the destination file if it already exists. For that purpose, call xtd::ioo::file_info::move_to(ustring, bool) instead.

◆ name()

xtd::ustring xtd::io::file_info::name ( ) const
overridevirtual

Gets the name of the file.

Returns
The name of the file.
Example
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
int main() {
// Create a reference to the current directory.
directory_info di(environment::current_directory());
// Create an array representing the files in the current directory.
vector<file_info> fi = di.get_files();
console::write_line("The following files exist in the current directory:");
// Print out the names of the files in the current directory.
for (file_info fi_temp : fi)
console::write_line(fi_temp.name());
}
// This code produces output similar to the following;
// results may vary based on the computer/file structure/etc.:
//
// The following files exist in the current directory:
// fileinfoname.exe
// fileinfoname.pdb
// newTemp.txt
Remarks
When first called, xtd::io::file_info calls Refresh and caches information about the file. On subsequent calls, you must call Refresh to get the latest copy of the information.
The name of the file includes the file extension.

Implements xtd::io::file_system_info.

◆ 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.
xrd::security::security_exceptionThe caller does not have the required permission.
xtd::unauthorized_access_exceptionThe path is a directory.
Example
The following example demonstrates the xtd::io::file_info::remove method.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
ustring path = R"(c:\MyTest.txt)";
file_info fi1(path);
try {
using_ (ofstream ofs = fi1.create_text()) {}
ustring 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 system_exception& e) {
console::write_line("The process failed: {0}", e.to_string());
}
}
};
// 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.
Example
The following example creates, closes, and deletes a file.
#include <xtd/xtd>
using namespace std;
using namespace xtd::io;
class program {
public:
static void main() {
// Create a reference to a file.
file_info fi("temp.txt");
// Actually create the file.
ofstream ofs = fi.create();
// Modify the file as required, and then close the file.
ofs.close();
// Delete the file.
fi.remove();
}
};
startup_(program);
Remarks
If the file does not exist, this method does nothing.

Implements xtd::io::file_system_info.

Member Data Documentation

◆ empty

const file_info xtd::io::file_info::empty
static

Represents the uninitialized xtd::io::file_info object. This field is constant.


The documentation for this class was generated from the following file: