Home

Drives

 

Introduction to Drives

 

Description

A drive is a physical device attached to a computer so it can store information. A drive can be a hard disk, a CD ROM, a DVD ROM, a flash (USB) drive, a memory card etc:

 
Hard Drive DVD Drive USB Flash Drive
Hard Drive DVD Drive USB Flash Drive
      
Floppy Drive Memory Card
Floppy Drive Flash Memory

A drive can reside inside a computer. That's the case for internal hard drives and most CD or DVD drives. A drive can also reside outside. That's the case for most flash (USB) drives. There are also versions of external hard drives and external DVD drives:

Hard Drive Floppy Drive USB Flash Drive
External Hard Drive External Floppy Drive USB Flash Drive Holder

A drive is referred to as virtual if it is not a real physical object. For example, a hard drive can be divided or partitioned internally, giving birth to each part that acts as its own drive.

While most drives are connected to a computer, a device connected to another computer can also be used as a drive. In this case, while the drive is connected to a computer A, a computer B must be connected to the computer A in order to use the drive on computer A. This is the case in computer networks where drives (or their contents) are shared.

Not all computers have the same drives and not all computers deal with the same means of storing data. Still, to simplify their identification, all objects used to hold data are referred to as drives. Because there are different ways to consider drives, there are also various means of accessing them.

There are two techniques of referring to drives. A drive that is directly connected to a computer, whether internally or externally, is referred to as a local drive. In Microsoft Windows, a local drive is represented by a letter, in uppercase, followed by a colon ":", and a backslash "\". Traditionally, drive A:\ is used for a 3.5" floppy drive that uses 3.5" floppy disks. Most computers nowadays don't (or hardly) use floppy disks. That drive is almost never used but, because of legacy, it is still represented in the Microsoft Windows operating system. Also, traditionally, drive B:\ was used for a 5.25" floppy drive that used 5.25" floppy disks. These disks have almost disappeared completely. Based on operating system legacy, in some computers, drive B:\ is still represented in computers (many computers don't show any drive B:\ anymore). Drive C:\ usually represents the main hard drive of a computer. The other letters assigned to the other drive are not standard; they vary from one computer to another. If a hard disk is partitioned, each partition uses its own letter and is represented as its own drive.

Getting the List of Drives of a Computer

Normally, you will hardly be concerned with the creation of drives. The operating system "creates" or assigns a drive whenever it juges it necessary. For example, as soon as you connect a USB drive to a port, the operating system automatically creates a drive and assigns a lette to it. You will only need to identify the drives that are available on a computer on which  your application is running. One of the primary operations you will perform consists of getting a list of drives on the computer.

The .NET Framework provides many classes that can assist you to identify the drives of a computer. To start, the Environment class is equipped with a member function named GetLogicalDrives. Its syntax is:

public:
    static array<String^>^ GetLogicalDrives();

When called, this member function produces an array of string where each element represents a logical in the computer. You can then use the drives as you see fit. 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;

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<String ^> ^ strDrives = Environment::GetLogicalDrives();

        for each(String ^ strDrive in strDrives)
            MessageBox::Show("Logical Drive: " + strDrive,
                             "Logical Drives",
                             MessageBoxButtons::OK,
                             MessageBoxIcon::Information);
    }
};

int APIENTRY WinMain(HINSTANCE hInstance,
		     HINSTANCE hPrevInstance,
		     LPSTR lpCmdLine,
		     int nCmdShow)
{
    Application::Run(gcnew CExercise);

    return 0;
}

Besides the Environment::GetLogicalDrives() member function, the DriveInfo class provides its own means of getting a list of logical drives on the local computer. This is done through the GetDrives(). Its syntax is:

public:
    static array<DriveInfo^>^ GetDrives();

As opposed to a list of strings, the DriveInfo::GetDrives() member function produces an array of DriveInfo objects, which is an array of logical drives on the computer. Here is an example of calling this member function:

array<DriveInfo ^> ^ diLocalDrives = DriveInfo::GetDrives();
 
 
 

Characteristics of Drives

 

Introduction

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:\\");

The Name of a Drive

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;
}

The Type of a Drive

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:

Member Description
Unknown The drive is unrecognizable
NoRootDirectory The root of the drive is unrecognizable
Removable This can be a floppy drive, a USB drive, a memory card, etc. A drive that can be removed at will
Fixed This is a hard drive or a partition on an HD
Network This is a network drive, usually located on another computer
CDROM This is drive CD or DVD drive
Ram This is the random access memory

The Type of Format

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:

Drive Information

The Capacity of a Drive

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);
}
 
 
   
 

Home Copyright © 2010-2012 FunctionX