xtd 0.2.0
Loading...
Searching...
No Matches
xtd::io::file_info Class Reference
Inheritance diagram for xtd::io::file_info:
xtd::io::file_system_info xtd::abstract_object xtd::object

Definition

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

Provides static methods for the creation, copying, deletion, moving, and opening of files,...
Definition file_info.h:41
Provides the base class for both xtd::io::file_info and xtd::io::directory_info objects.
Definition file_system_info.h:87
#define core_export_
Define shared library export.
Definition core_export.h:13
Inheritance
xtd::io::file_system_infoxtd::io::file_info
Header
#include <xtd/io/file_info>
Namespace
xtd::io
Library
xtd.core
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.
Performs operations on std::basic_string instances that contain file or directory path information....
Definition path.h:36
Defines the base class for predefined exceptions in the xtd namespace.
Definition system_exception.h:24
#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
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 xtd::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 xtd::io::file_info members such as xtd::io::file_info::open, xtd::io::file_info::open_read, xtd::io::file_info::open_text, xtd::io::file_info::create_text, or xtd::io::file_info::create.
By default, full read/write access to new files is granted to all users.
Examples
file_info.cpp, file_info2.cpp, file_info_append_text.cpp, file_info_move_to.cpp, file_info_open.cpp, file_info_open_read.cpp, file_info_open_text.cpp, file_info_open_write.cpp, and file_info_replace.cpp.

Public Fields

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

Public Constructors

 file_info (const xtd::string &file_name)
 Initializes a new instance of the xtd::io::file_info class, which acts as a wrapper for a file path.
 

Public Properties

xtd::io::directory_info directory () const
 Gets an instance of the parent directory.
 
xtd::string directory_name () const
 Gets a string representing the directory's full path.
 
bool exists () const override
 Gets a value indicating whether a file exists.
 
bool is_read_only () const
 Gets a value that determines if the current file is read only.
 
void is_read_only (bool value)
 Sets a value that determines if the current file is read only.
 
size_t length () const
 Gets the size, in bytes, of the current file.
 
xtd::string name () const override
 Gets the name of the file.
 

Public Methods

xtd::io::stream_writer append_text () const
 Creates a xtd::io::stream_writer that appends text to the file represented by this instance of the xtd::io::file_info.
 
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.
 
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.
 
std::ofstream create () const
 Creates a file.
 
xtd::io::stream_writer create_text () const
 Creates a std::ofstream that writes a new text file.
 
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.
 
void move_to (const xtd::string &dest_file_name, bool overwrite)
 Moves a specified file to a new location, providing the options to specify a new file name and to overwrite the destination file if it already exists.
 
std::fstream open (std::ios::openmode mode) const
 Opens a file in the specified mode.
 
std::ifstream open_read () const
 Creates a read-only std::ifstream.
 
xtd::io::stream_reader open_text () const
 Creates a xtd::io::stream_reader that reads from an existing text file.
 
std::ofstream open_write () const
 Creates a write-only std::ofstream.
 
void remove () const override
 Permanently deletes a file.
 
file_info replace (const xtd::string &destination_file_name, const xtd::string &destination_backup_file_name)
 Replaces the contents of a specified file with the file described by the current xtd::io::file_info object, deleting the original file, and creating a backup of the replaced file.
 

Additional Inherited Members

- 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.
 
xtd::io::file_system_infoattributes (xtd::io::file_attributes value)
 Sets the attributes for the current file or directory.
 
const xtd::date_timecreation_time () const
 Gets the creation time of the current file or directory.
 
xtd::io::file_system_infocreation_time (const xtd::date_time &value)
 Gets the creation time of the current file or directory.
 
xtd::date_time creation_time_utc () const
 Gets the creation time, in coordinated universal time (UTC), of the current file or directory.
 
xtd::io::file_system_infocreation_time_utc (const xtd::date_time &value)
 Sets the creation time, in coordinated universal time (UTC), of the current file or directory.
 
virtual xtd::string 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.
 
xtd::string full_name () const
 Gets the full path of the directory or file.
 
const xtd::date_timelast_access_time () const
 Gets the time the current file or directory was last accessed.
 
xtd::io::file_system_infolast_access_time (const xtd::date_time &value)
 Sets the time the current file or directory was last accessed.
 
xtd::date_time last_access_time_utc () const
 Gets the time, in coordinated universal time (UTC), that the current file or directory was last accessed.
 
xtd::io::file_system_infolast_access_time_utc (const xtd::date_time &value)
 Sets the time, in coordinated universal time (UTC), that the current file or directory was last accessed.
 
const xtd::date_timelast_write_time () const
 Gets the time when the current file or directory was last written to.
 
xtd::io::file_system_infolast_write_time (const xtd::date_time &value)
 Sets the time when the current file or directory was last written to.
 
xtd::date_time last_write_time_utc () const
 Gets the time, in coordinated universal time (UTC), when the current file or directory was last written to.
 
xtd::io::file_system_infolast_write_time_utc (const xtd::date_time &value)
 Sets the time, in coordinated universal time (UTC), when the current file or directory was last written to.
 
xtd::io::file_permissions permissions () const
 Gets the permissions for the current file or directory.
 
xtd::io::file_system_infopermissions (xtd::io::file_permissions value)
 Sets the permissions for the current file or directory.
 
void refresh ()
 Refreshes the state of the object.
 
xtd::string 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.
 
- Public Member Functions inherited from xtd::object
 object ()=default
 Create a new instance of the ultimate base class object.
 
virtual bool equals (const object &obj) const noexcept
 Determines whether the specified object is equal to the current object.
 
virtual size_t get_hash_code () const noexcept
 Serves as a hash function for a particular type.
 
virtual type_object get_type () const noexcept
 Gets the type of the current instance.
 
template<typename object_t >
xtd::uptr< object_t > memberwise_clone () const
 Creates a shallow copy of the current object.
 
- Static Public Member Functions inherited from xtd::object
template<typename object_a_t , typename object_b_t >
static bool equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are considered equal.
 
template<typename object_a_t , typename object_b_t >
static bool reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are the same instance.
 
- Protected Attributes inherited from xtd::io::file_system_info
xtd::string full_path_
 Represents the fully qualified path of the directory or file.
 
xtd::string original_path_
 The path originally specified by the user, whether relative or absolute.
 
- 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.
 
- Protected Member Functions inherited from xtd::abstract_object
 abstract_object ()=default
 Initializes a new instance of the xtd::abstract_object class.
 

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