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 | Static Public Member Functions | List of all members
xtd::io::drive_info Class Reference

#include <drive_info.h>

Definition

Provides access to information on a drive.

class drive_info : public xtd::object

Ineheritance xtd::objectxtd::io::drive_info

Namespace
xtd::io
Library
xtd.core
Example
The following code example demonstrates the use of the xtd::io::drive_info class to display information about all of the drives on the current system.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
vector<drive_info> all_drives = drive_info::get_drives();
for (drive_info d : all_drives) {
console::write_line("Drive {0}", d.name());
console::write_line(" Drive type: {0}", d.drive_type());
if (d.is_ready() == true) {
console::write_line(" Volume label: {0}", d.volume_label());
console::write_line(" File system: {0}", d.drive_format());
console::write_line(" Available space to current user:{0, 15} bytes", d.available_free_space());
console::write_line(" Total available space: {0, 15} bytes", d.total_free_space());
console::write_line(" Total size of drive: {0, 15} bytes", d.total_size());
}
}
}
};
startup_(program);
// This code produces the following output :
//
// Drive A:\
// Drive type: Removable
// Drive C:\
// Drive type: Fixed
// Volume label:
// File system: FAT32
// Available space to current user: 4770430976 bytes
// Total available space: 4770430976 bytes
// Total size of drive: 10731683840 bytes
// Drive D:\
// Drive type: Fixed
// Volume label:
// File system: NTFS
// Available space to current user: 15114977280 bytes
// Total available space: 15114977280 bytes
// Total size of drive: 25958948864 bytes
// Drive E:\
// Drive type: CDRom
//
// The actual output of this code will vary based on machine and the permissions
// granted to the user executing it.
Remarks
This class models a drive and provides methods and properties to query for drive information. Use xtd::io::drive_info to determine what drives are available, and what type of drives they are. You can also query to determine the capacity and available free space on the drive.

Static Public Attributes

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

Public Member Functions

 drive_info (const xtd::ustring &drive_name)
 Provides access to information on the specified drive. More...
 
size_t available_free_space () const
 Indicates the amount of available free space on a drive, in bytes. More...
 
xtd::ustring drive_format () const
 Gets the name of the file system, such as NTFS or FAT32. More...
 
xtd::io::drive_type drive_type () const
 Gets the drive type, such as CD-ROM, removable, network, or fixed. More...
 
bool is_ready () const noexcept
 Gets a value that indicates whether a drive is ready. More...
 
xtd::ustring name () const noexcept
 Gets the name of a drive, such as C:. More...
 
xtd::io::directory_info root_directory () const noexcept
 Gets the root directory of a drive. More...
 
xtd::ustring to_string () const noexcept override
 Returns a drive name as a string. More...
 
size_t total_free_space () const
 Gets the total amount of free space available on a drive, in bytes. More...
 
size_t total_size () const
 Gets the total size of storage space on a drive, in bytes. More...
 
xtd::ustring volume_label () const
 Gets the volume label of a drive. More...
 
- Public Member Functions inherited from xtd::object
 object ()=default
 Create a new instance of the ultimate base class object. More...
 
virtual bool equals (const object &obj) const noexcept
 Determines whether the specified object is equal to the current object. More...
 
virtual size_t get_hash_code () const noexcept
 Serves as a hash function for a particular type. More...
 
template<typename object_t >
std::unique_ptr< object_t > memberwise_clone () const
 Gets the type of the current instance. More...
 

Static Public Member Functions

static std::vector< xtd::io::drive_infoget_drives () noexcept
 Retrieves the drive names of all logical drives on a computer. More...
 
- Static Public Member Functions inherited from xtd::object
static bool equals (const object &object_a, const object &object_b) noexcept
 Determines whether the specified object instances are considered equal. More...
 
static bool reference_equals (const object &object_a, const object &object_b) noexcept
 Determines whether the specified object instances are the same instance. More...
 

Constructor & Destructor Documentation

◆ drive_info()

xtd::io::drive_info::drive_info ( const xtd::ustring drive_name)

Provides access to information on the specified drive.

Parameters
drive_nameA valid drive path or drive letter. This can be either uppercase or lowercase, 'a' to 'z'. A null value is not valid.
Exceptions
xtd::argument_exceptiondrive_name does not refer to a valid drive.
Remarks
Use this class to obtain information on drives. The drive name must be either an uppercase or lowercase letter from 'a' to 'z'. You cannot use this method to obtain information on drive names that are null or use UNC (\server) paths.

Member Function Documentation

◆ available_free_space()

size_t xtd::io::drive_info::available_free_space ( ) const

Indicates the amount of available free space on a drive, in bytes.

Returns
The amount of free space available on the drive, in bytes.
Exceptions
xtd::unauthorized_access_exceptionAccess to the drive information is denied.
xtd::io::drive_not_foundThe drive does not exist or is not mapped.
xtd::io::io_exceptionAn I/O error occurred (for example, a disk error or a drive was not ready).
Example
The following code example demonstrates the use of the xtd::io::drive_info class to display information about all of the drives on the current system.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
vector<drive_info> all_drives = drive_info::get_drives();
for (drive_info d : all_drives) {
console::write_line("Drive {0}", d.name());
console::write_line(" Drive type: {0}", d.drive_type());
if (d.is_ready() == true) {
console::write_line(" Volume label: {0}", d.volume_label());
console::write_line(" File system: {0}", d.drive_format());
console::write_line(" Available space to current user:{0, 15} bytes", d.available_free_space());
console::write_line(" Total available space: {0, 15} bytes", d.total_free_space());
console::write_line(" Total size of drive: {0, 15} bytes", d.total_size());
}
}
}
};
startup_(program);
// This code produces the following output :
//
// Drive A:\
// Drive type: Removable
// Drive C:\
// Drive type: Fixed
// Volume label:
// File system: FAT32
// Available space to current user: 4770430976 bytes
// Total available space: 4770430976 bytes
// Total size of drive: 10731683840 bytes
// Drive D:\
// Drive type: Fixed
// Volume label:
// File system: NTFS
// Available space to current user: 15114977280 bytes
// Total available space: 15114977280 bytes
// Total size of drive: 25958948864 bytes
// Drive E:\
// Drive type: CDRom
//
// The actual output of this code will vary based on machine and the permissions
// granted to the user executing it.
Remarks
This property indicates the amount of free space available on the drive. Note that this number may be different from the xtd::io::drive_info::total_free_space number because this property takes into account disk quotas.

◆ drive_format()

xtd::ustring xtd::io::drive_info::drive_format ( ) const

Gets the name of the file system, such as NTFS or FAT32.

Returns
The name of the file system on the specified drive.
Exceptions
xtd::unauthorized_access_exceptionAccess to the drive information is denied.
xtd::io::drive_not_foundThe drive does not exist or is not mapped.
xtd::io::io_exceptionAn I/O error occurred (for example, a disk error or a drive was not ready).
Example
The following code example demonstrates the use of the xtd::io::drive_info class to display information about all of the drives on the current system.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
vector<drive_info> all_drives = drive_info::get_drives();
for (drive_info d : all_drives) {
console::write_line("Drive {0}", d.name());
console::write_line(" Drive type: {0}", d.drive_type());
if (d.is_ready() == true) {
console::write_line(" Volume label: {0}", d.volume_label());
console::write_line(" File system: {0}", d.drive_format());
console::write_line(" Available space to current user:{0, 15} bytes", d.available_free_space());
console::write_line(" Total available space: {0, 15} bytes", d.total_free_space());
console::write_line(" Total size of drive: {0, 15} bytes", d.total_size());
}
}
}
};
startup_(program);
// This code produces the following output :
//
// Drive A:\
// Drive type: Removable
// Drive C:\
// Drive type: Fixed
// Volume label:
// File system: FAT32
// Available space to current user: 4770430976 bytes
// Total available space: 4770430976 bytes
// Total size of drive: 10731683840 bytes
// Drive D:\
// Drive type: Fixed
// Volume label:
// File system: NTFS
// Available space to current user: 15114977280 bytes
// Total available space: 15114977280 bytes
// Total size of drive: 25958948864 bytes
// Drive E:\
// Drive type: CDRom
//
// The actual output of this code will vary based on machine and the permissions
// granted to the user executing it.
Remarks
Use xtd::io::drive_info::drive_format to determine what formatting a drive uses.

◆ drive_type()

xtd::io::drive_type xtd::io::drive_info::drive_type ( ) const

Gets the drive type, such as CD-ROM, removable, network, or fixed.

Returns
One of the enumeration values that specifies a drive type.
Example
The following code example demonstrates the use of the xtd::io::drive_info class to display information about all of the drives on the current system.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
vector<drive_info> all_drives = drive_info::get_drives();
for (drive_info d : all_drives) {
console::write_line("Drive {0}", d.name());
console::write_line(" Drive type: {0}", d.drive_type());
if (d.is_ready() == true) {
console::write_line(" Volume label: {0}", d.volume_label());
console::write_line(" File system: {0}", d.drive_format());
console::write_line(" Available space to current user:{0, 15} bytes", d.available_free_space());
console::write_line(" Total available space: {0, 15} bytes", d.total_free_space());
console::write_line(" Total size of drive: {0, 15} bytes", d.total_size());
}
}
}
};
startup_(program);
// This code produces the following output :
//
// Drive A:\
// Drive type: Removable
// Drive C:\
// Drive type: Fixed
// Volume label:
// File system: FAT32
// Available space to current user: 4770430976 bytes
// Total available space: 4770430976 bytes
// Total size of drive: 10731683840 bytes
// Drive D:\
// Drive type: Fixed
// Volume label:
// File system: NTFS
// Available space to current user: 15114977280 bytes
// Total available space: 15114977280 bytes
// Total size of drive: 25958948864 bytes
// Drive E:\
// Drive type: CDRom
//
// The actual output of this code will vary based on machine and the permissions
// granted to the user executing it.
Remarks
The xtd::io::drive_info::drive_type property indicates whether a drive is one of the following: CDRom, Fixed, Network, NoRootDirectory, Ram, Removable, or Unknown. These values are described in the xtd::io::drive_type enumeration.

◆ get_drives()

static std::vector<xtd::io::drive_info> xtd::io::drive_info::get_drives ( )
staticnoexcept

Retrieves the drive names of all logical drives on a computer.

Returns
An array of type xtd::ioo::drive_info that represents the logical drives on a computer.
Exceptions
xtd::unauthorized_access_exceptionAccess to the drive information is denied.
xtd::io::io_exceptionAn I/O error occurred (for example, a disk error or a drive was not ready).
Example
The following code example demonstrates the use of the xtd::io::drive_info class to display information about all of the drives on the current system.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
vector<drive_info> all_drives = drive_info::get_drives();
for (drive_info d : all_drives) {
console::write_line("Drive {0}", d.name());
console::write_line(" Drive type: {0}", d.drive_type());
if (d.is_ready() == true) {
console::write_line(" Volume label: {0}", d.volume_label());
console::write_line(" File system: {0}", d.drive_format());
console::write_line(" Available space to current user:{0, 15} bytes", d.available_free_space());
console::write_line(" Total available space: {0, 15} bytes", d.total_free_space());
console::write_line(" Total size of drive: {0, 15} bytes", d.total_size());
}
}
}
};
startup_(program);
// This code produces the following output :
//
// Drive A:\
// Drive type: Removable
// Drive C:\
// Drive type: Fixed
// Volume label:
// File system: FAT32
// Available space to current user: 4770430976 bytes
// Total available space: 4770430976 bytes
// Total size of drive: 10731683840 bytes
// Drive D:\
// Drive type: Fixed
// Volume label:
// File system: NTFS
// Available space to current user: 15114977280 bytes
// Total available space: 15114977280 bytes
// Total size of drive: 25958948864 bytes
// Drive E:\
// Drive type: CDRom
//
// The actual output of this code will vary based on machine and the permissions
// granted to the user executing it.
Remarks
This method retrieves all logical drive names on a computer. You can use this information to iterate through the array and obtain information on the drives using other xtd::io::drive_info methods and properties. Use the xtd::io::drive_info::is_ready property to test whether a drive is ready because using this method on a drive that is not ready will throw a xtd::io::io_exception.

◆ is_ready()

bool xtd::io::drive_info::is_ready ( ) const
noexcept

Gets a value that indicates whether a drive is ready.

Returns
true if the drive is ready; false if the drive is not ready.
Example
The following code example demonstrates the use of the xtd::io::drive_info class to display information about all of the drives on the current system.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
vector<drive_info> all_drives = drive_info::get_drives();
for (drive_info d : all_drives) {
console::write_line("Drive {0}", d.name());
console::write_line(" Drive type: {0}", d.drive_type());
if (d.is_ready() == true) {
console::write_line(" Volume label: {0}", d.volume_label());
console::write_line(" File system: {0}", d.drive_format());
console::write_line(" Available space to current user:{0, 15} bytes", d.available_free_space());
console::write_line(" Total available space: {0, 15} bytes", d.total_free_space());
console::write_line(" Total size of drive: {0, 15} bytes", d.total_size());
}
}
}
};
startup_(program);
// This code produces the following output :
//
// Drive A:\
// Drive type: Removable
// Drive C:\
// Drive type: Fixed
// Volume label:
// File system: FAT32
// Available space to current user: 4770430976 bytes
// Total available space: 4770430976 bytes
// Total size of drive: 10731683840 bytes
// Drive D:\
// Drive type: Fixed
// Volume label:
// File system: NTFS
// Available space to current user: 15114977280 bytes
// Total available space: 15114977280 bytes
// Total size of drive: 25958948864 bytes
// Drive E:\
// Drive type: CDRom
//
// The actual output of this code will vary based on machine and the permissions
// granted to the user executing it.
Remarks
xtd::io::drive_info::is_ready indicates whether a drive is ready. For example, it indicates whether a CD is in a CD drive or whether a removable storage device is ready for read/write operations. If you do not test whether a drive is ready, and it is not ready, querying the drive using xtd::io::drive_info will raise an xtd::io::io_exception.
Do not rely on xtd::io::drive_info::is_ready to avoid catching exceptions from other members such as xtd::io::drive_info::total_size, xtd::io::drive_info::total_free_space, and xtd::io::drive_info::drive_format. Between the time that your code checks xtd::io::drive_info::is_ready and then accesses one of the other properties (even if the access occurs immediately after the check), a drive may have been disconnected or a disk may have been removed.

◆ name()

xtd::ustring xtd::io::drive_info::name ( ) const
noexcept

Gets the name of a drive, such as C:.

Returns
The name of the drive.
Example
The following code example demonstrates the use of the xtd::io::drive_info class to display information about all of the drives on the current system.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
vector<drive_info> all_drives = drive_info::get_drives();
for (drive_info d : all_drives) {
console::write_line("Drive {0}", d.name());
console::write_line(" Drive type: {0}", d.drive_type());
if (d.is_ready() == true) {
console::write_line(" Volume label: {0}", d.volume_label());
console::write_line(" File system: {0}", d.drive_format());
console::write_line(" Available space to current user:{0, 15} bytes", d.available_free_space());
console::write_line(" Total available space: {0, 15} bytes", d.total_free_space());
console::write_line(" Total size of drive: {0, 15} bytes", d.total_size());
}
}
}
};
startup_(program);
// This code produces the following output :
//
// Drive A:\
// Drive type: Removable
// Drive C:\
// Drive type: Fixed
// Volume label:
// File system: FAT32
// Available space to current user: 4770430976 bytes
// Total available space: 4770430976 bytes
// Total size of drive: 10731683840 bytes
// Drive D:\
// Drive type: Fixed
// Volume label:
// File system: NTFS
// Available space to current user: 15114977280 bytes
// Total available space: 15114977280 bytes
// Total size of drive: 25958948864 bytes
// Drive E:\
// Drive type: CDRom
//
// The actual output of this code will vary based on machine and the permissions
// granted to the user executing it.
Remarks
This property is the name assigned to the drive, such as C:\ or E:.

◆ root_directory()

xtd::io::directory_info xtd::io::drive_info::root_directory ( ) const
noexcept

Gets the root directory of a drive.

Returns
An object that contains the root directory of the drive.

◆ to_string()

xtd::ustring xtd::io::drive_info::to_string ( ) const
overridevirtualnoexcept

Returns a drive name as a string.

Returns
The name of the drive.
Remarks
This method returns the xtd::io::drive_info::name property.

Reimplemented from xtd::object.

◆ total_free_space()

size_t xtd::io::drive_info::total_free_space ( ) const

Gets the total amount of free space available on a drive, in bytes.

Returns
The total free space available on a drive, in bytes.
Exceptions
xtd::unauthorized_access_exceptionAccess to the drive information is denied.
xtd::io::drive_not_foundThe drive does not exist or is not mapped.
xtd::io::io_exceptionAn I/O error occurred (for example, a disk error or a drive was not ready).
Example
The following code example demonstrates the use of the xtd::io::drive_info class to display information about all of the drives on the current system.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
vector<drive_info> all_drives = drive_info::get_drives();
for (drive_info d : all_drives) {
console::write_line("Drive {0}", d.name());
console::write_line(" Drive type: {0}", d.drive_type());
if (d.is_ready() == true) {
console::write_line(" Volume label: {0}", d.volume_label());
console::write_line(" File system: {0}", d.drive_format());
console::write_line(" Available space to current user:{0, 15} bytes", d.available_free_space());
console::write_line(" Total available space: {0, 15} bytes", d.total_free_space());
console::write_line(" Total size of drive: {0, 15} bytes", d.total_size());
}
}
}
};
startup_(program);
// This code produces the following output :
//
// Drive A:\
// Drive type: Removable
// Drive C:\
// Drive type: Fixed
// Volume label:
// File system: FAT32
// Available space to current user: 4770430976 bytes
// Total available space: 4770430976 bytes
// Total size of drive: 10731683840 bytes
// Drive D:\
// Drive type: Fixed
// Volume label:
// File system: NTFS
// Available space to current user: 15114977280 bytes
// Total available space: 15114977280 bytes
// Total size of drive: 25958948864 bytes
// Drive E:\
// Drive type: CDRom
//
// The actual output of this code will vary based on machine and the permissions
// granted to the user executing it.
Remarks
This property indicates the total amount of free space available on the drive, not just what is available to the current user.

◆ total_size()

size_t xtd::io::drive_info::total_size ( ) const

Gets the total size of storage space on a drive, in bytes.

Returns
The total size of the drive, in bytes.
Exceptions
xtd::unauthorized_access_exceptionAccess to the drive information is denied.
xtd::io::drive_not_foundThe drive does not exist or is not mapped.
xtd::io::io_exceptionAn I/O error occurred (for example, a disk error or a drive was not ready).
Example
The following code example demonstrates the use of the xtd::io::drive_info class to display information about all of the drives on the current system.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
vector<drive_info> all_drives = drive_info::get_drives();
for (drive_info d : all_drives) {
console::write_line("Drive {0}", d.name());
console::write_line(" Drive type: {0}", d.drive_type());
if (d.is_ready() == true) {
console::write_line(" Volume label: {0}", d.volume_label());
console::write_line(" File system: {0}", d.drive_format());
console::write_line(" Available space to current user:{0, 15} bytes", d.available_free_space());
console::write_line(" Total available space: {0, 15} bytes", d.total_free_space());
console::write_line(" Total size of drive: {0, 15} bytes", d.total_size());
}
}
}
};
startup_(program);
// This code produces the following output :
//
// Drive A:\
// Drive type: Removable
// Drive C:\
// Drive type: Fixed
// Volume label:
// File system: FAT32
// Available space to current user: 4770430976 bytes
// Total available space: 4770430976 bytes
// Total size of drive: 10731683840 bytes
// Drive D:\
// Drive type: Fixed
// Volume label:
// File system: NTFS
// Available space to current user: 15114977280 bytes
// Total available space: 15114977280 bytes
// Total size of drive: 25958948864 bytes
// Drive E:\
// Drive type: CDRom
//
// The actual output of this code will vary based on machine and the permissions
// granted to the user executing it.
Remarks
This property indicates the total size of the drive in bytes, not just what is available to the current user.

◆ volume_label()

xtd::ustring xtd::io::drive_info::volume_label ( ) const

Gets the volume label of a drive.

Returns
The volume label.
Exceptions
xtd::unauthorized_access_exceptionAccess to the drive information is denied.
xtd::io::drive_not_foundThe drive does not exist or is not mapped.
xtd::io::io_exceptionAn I/O error occurred (for example, a disk error or a drive was not ready).
Example
The following code example demonstrates the use of the xtd::io::drive_info class to display information about all of the drives on the current system.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
public:
static void main() {
vector<drive_info> all_drives = drive_info::get_drives();
for (drive_info d : all_drives) {
console::write_line("Drive {0}", d.name());
console::write_line(" Drive type: {0}", d.drive_type());
if (d.is_ready() == true) {
console::write_line(" Volume label: {0}", d.volume_label());
console::write_line(" File system: {0}", d.drive_format());
console::write_line(" Available space to current user:{0, 15} bytes", d.available_free_space());
console::write_line(" Total available space: {0, 15} bytes", d.total_free_space());
console::write_line(" Total size of drive: {0, 15} bytes", d.total_size());
}
}
}
};
startup_(program);
// This code produces the following output :
//
// Drive A:\
// Drive type: Removable
// Drive C:\
// Drive type: Fixed
// Volume label:
// File system: FAT32
// Available space to current user: 4770430976 bytes
// Total available space: 4770430976 bytes
// Total size of drive: 10731683840 bytes
// Drive D:\
// Drive type: Fixed
// Volume label:
// File system: NTFS
// Available space to current user: 15114977280 bytes
// Total available space: 15114977280 bytes
// Total size of drive: 25958948864 bytes
// Drive E:\
// Drive type: CDRom
//
// The actual output of this code will vary based on machine and the permissions
// granted to the user executing it.
Remarks
The label length is determined by the operating system. For example, NTFS allows a volume label to be up to 32 characters long.

Member Data Documentation

◆ empty

const drive_info xtd::io::drive_info::empty
static

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


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