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

◆ 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.
Examples
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 string& 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;
}
The exception that is thrown when one of the arguments provided to a method is not valid.
Definition argument_exception.h:23
size_type size() const noexcept
Returns the number of char_t elements in the string, i.e. std::distance(begin(), end()).
Definition basic_string.h:834
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 attempt to access a file that does not exist on disk fails.
Definition file_not_found_exception.h:29
#define csf_
Provides information about the current stack frame.
Definition current_stack_frame.h:30
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.

Examples
file_info2.cpp, and file_info_move_to.cpp.