In microsoft Windows, a local drive is represented by a letter followed by :\. For example, in most personal computers, the (main) hard drive is represented as C:\. The main class used to manage the drives of a computer is named DriveInfo. If you know the drive you want to use or access, the DriveInfo class provides a constructor that allows you to get a reference to that drive. This is the only constructor of this class. The syntax of the constructor is: public: DriveInfo(String^ driveName); If you want to access a drive, you can declare a DriveInfo variable and pass the drive letter to this constructor. Here is an example: DriveInfo ^ diHardDrive = gcnew DriveInfo("C:\\");
If you want to get the name of a drive, you can access the Name property of the DriveInfo class: public: property String^ Name { String^ get (); } Here is an example: #include <windows.h> #using <System.dll> #using <System.Drawing.dll> #using <System.Windows.Forms.dll> using namespace System; using namespace System::Drawing; using namespace System::Windows::Forms; using namespace System::IO; public ref class CExercise : public Form { private: Button ^ btnDrives; public: CExercise() { InitializeComponent(); } private: void InitializeComponent() { btnDrives = gcnew Button; btnDrives->Location = Point(20, 20); btnDrives->Text = "Drives"; btnDrives->Click += gcnew EventHandler(this, &CExercise::ShowDrives); Controls->Add(btnDrives); Text = L"Exercise"; StartPosition = FormStartPosition::CenterScreen; } void ShowDrives(Object ^ sender, EventArgs ^ e) { array<DriveInfo ^> ^ diLocalDrives = DriveInfo::GetDrives(); for each(DriveInfo ^ di in diLocalDrives) MessageBox::Show("Logical Drive: " + di->Name, "Logical Drives", MessageBoxButtons::OK, MessageBoxIcon::Information); } }; int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { Application::Run(gcnew CExercise); return 0; }
A drive is primarily recognized by its category. Examples of categories are hard drives, CD and DVD drives, etc. The categories of drives are stored in the DriveType property of the DriveInfo class. The DriveType property is based on an enumeration of the same name: public: property DriveType DriveType { DriveType get (); } Its members are:
The format system is the scheme that a computer (actually the operating system) uses to store and process the values in its drives. Microsoft Windows uses various types of formats, including FAT32 and NTFS. To know the format scheme that a drive is using, get the value of the DriveFormat property of the DriveInfo class: public: property String^ DriveFormat { String^ get (); } Here is an example of accessing this property: #include <windows.h> #using <System.dll> #using <System.Drawing.dll> #using <System.Windows.Forms.dll> using namespace System; using namespace System::Drawing; using namespace System::Windows::Forms; using namespace System::IO; public ref class CExercise : public Form { private: Button ^ btnDrive; public: CExercise() { InitializeComponent(); } private: void InitializeComponent() { btnDrive = gcnew Button; btnDrive->Location = Point(20, 20); btnDrive->Text = "Drive"; btnDrive->Click += gcnew EventHandler(this, &CExercise::CheckDrive); Controls->Add(btnDrive); Text = L"Exercise"; StartPosition = FormStartPosition::CenterScreen; } void CheckDrive(Object ^ sender, EventArgs ^ e) { DriveInfo ^ diHardDrive = gcnew DriveInfo("C:\\"); if( diHardDrive != nullptr ) MessageBox::Show("Operating System: " + Environment::OSVersion + Environment::NewLine + "Drive Format: " + diHardDrive->DriveFormat, "Exercise", MessageBoxButtons::OK, MessageBoxIcon::Information); } }; int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { Application::Run(gcnew CExercise); return 0; } Run on Microsoft Windows 7 Ultimate, this would produce:
A drive is primarily used to hold some values. The capacity of a drive is the amount of data it can hold. This is usually measured in bits and bytes. As there are various types of drives, they also have different capacities. To help you to know the capacity of a drive, the DriveInfo class is equipped with a property named TotalSize: public: property long long TotalSize { long long get (); } This property produces a long integer that represents the normal total capacity of a drive. Here is an example of accessing it: void CheckDrive(Object ^ sender, EventArgs ^ e) { DriveInfo ^ diHardDrive = gcnew DriveInfo("C:\\"); if( diHardDrive != nullptr ) MessageBox::Show("You hard drive has a capacity of " + diHardDrive->TotalSize.ToString() + " bytes.", "Logical Drives", MessageBoxButtons::OK, MessageBoxIcon::Information); } As values are stored in a drive, it gets filled up. In some drives, values can be deleted, copied, or moved. This means that the capacity of some drives changes some time to time. At one time, to know the available free space of a drive, you can get the value of the AvailableFreeSpace property of its DriveInfo object: public: property long long AvailableFreeSpace { long long get (); } Here is an example of accessing this property: void CheckDrive(Object ^ sender, EventArgs ^ e) { DriveInfo ^ diHardDrive = gcnew DriveInfo("C:\\"); if( diHardDrive != nullptr ) MessageBox::Show("Hard Drive Capacity: " + diHardDrive->TotalSize.ToString() + " bytes.\n" + "Available Free Space: " + diHardDrive->AvailableFreeSpace.ToString() + " bytes.\n", "Exercise", MessageBoxButtons::OK, MessageBoxIcon::Information); } |
|
|||||||||||||||||||||||||||||
|