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:
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.
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 method named GetLogicalDrives. Its syntax is: public static string[] GetLogicalDrives() When called, this method 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: private void btnDrives_Click(object sender, EventArgs e)
{
string[] strDrives = Environment.GetLogicalDrives();
foreach (string strDrive in strDrives)
MessageBox.Show("Logical Drive: " + strDrive,
"Logical Drives",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
Besides the Environment.GetLogicalDrives() method, 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 DriveInfo[] GetDrives(); As opposed to a list of strings, the DriveInfo.GetDrives() method produces an array of DriveInfo objects, which is an array of logical drives on the computer. Here is an example of calling this method: private void btnDrives_Click(object sender, EventArgs e)
{
DriveInfo[] diLocalDrives = DriveInfo.GetDrives();
}
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: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace Drives1 { public partial class Exercise : Form { public Exercise() { InitializeComponent(); } private void btnDrives_Click(object sender, EventArgs e) { DriveInfo diHardDrive = new DriveInfo(@"C:\"); } } }
If you want to get the name of a drive, you can access the Name property of the DriveInfo class: public string Name { get; } Here is an example: private void btnDrives_Click(object sender, EventArgs e) { DriveInfo[] diLocalDrives = DriveInfo.GetDrives(); foreach (DriveInfo diLogicalDrive in diLocalDrives) MessageBox.Show("Logical Drive: " + diLogicalDrive.Name, "Logical Drives", MessageBoxButtons.OK, MessageBoxIcon.Information); }
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 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 string DriveFormat { get; } Here is an example of accessing this property: private void btnDrives_Click(object sender, EventArgs e) { DriveInfo diHardDrive = new DriveInfo(@"C:\"); if( diHardDrive != null ) MessageBox.Show("Operating System: " + Environment.OSVersion.ToString() + Environment.NewLine + "Drive Format: " + diHardDrive.DriveFormat, "Exercise", MessageBoxButtons.OK, MessageBoxIcon.Information); }
A drive is priarily 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 long TotalSize { get; } This property produces a long integer that represents the normal total capacity of a drive. Here is an example of accessing it: private void btnDrives_Click(object sender, EventArgs e) { DriveInfo diHardDrive = new DriveInfo(@"C:\"); if( diHardDrive != null ) 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 long AvailableFreeSpace { get; } Here is an example of accessing this property: private void btnDrives_Click(object sender, EventArgs e) { DriveInfo diHardDrive = new DriveInfo(@"C:\"); if( diHardDrive != null ) MessageBox.Show("Hard Drive Capacity: " + diHardDrive.TotalSize.ToString() + " bytes.\n" + "Available Free Space: " + diHardDrive.AvailableFreeSpace.ToString() + " bytes.\n", "Exercise", MessageBoxButtons.OK, MessageBoxIcon.Information); }
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||