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

◆ 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 xtd::io::file_info 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.
Examples
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 auto main() {
string 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 string& file_name, bool set_read_only) {
// Create a new xtd::io::file_info 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 string& file_name) {
// Create a new xtd::io::file_info object.
file_info f_info(file_name);
// Return the IsReadOnly property value.
return f_info.is_read_only();
}
};
startup_(program::main);
// 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
Provides static methods for the creation, copying, deletion, moving, and opening of files,...
Definition file_info.h:41
#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
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::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.