Home

Introduction

 

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 method named GetLogicalDrives. Its signature is:

static member GetLogicalDrives : unit -> string[]

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:

open System
open System.IO
open System.Drawing
open System.Windows.Forms

let exercise : Form = new Form(MaximizeBox = false, Text = "Exercise",
                               StartPosition = FormStartPosition.CenterScreen)

let lbxDrives : ListBox = new ListBox(Location = new Point(12, 12), Height = 130)

let strDrives = Environment.GetLogicalDrives()

for drive in strDrives do
    lbxDrives.Items.Add drive |> ignore
exercise.Controls.Add lbxDrives

[<STAThread>]
[<EntryPoint>]
let main argv = 
    Application.Run exercise
    0

This would produce:

Getting the List of Drives of a Computer

To supprt drives, the .NET Framework provides a class named DriveInfo:

type DriveInfo =  
    class 
        interface ISerializable 
    end

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 signature is:

static member GetDrives : unit -> DriveInfo[] 

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:

open System
open System.IO
open System.Drawing
open System.Windows.Forms

let exercise : Form = new Form(MaximizeBox = false, Text = "Exercise",
                               StartPosition = FormStartPosition.CenterScreen)

let lbxDrives : ListBox = new ListBox(Location = new Point(12, 12), Height = 130)

let drives : DriveInfo array = DriveInfo.GetDrives()

for drive in drives do
    lbxDrives.Items.Add drive |> ignore
exercise.Controls.Add lbxDrives

[<STAThread>]
[<EntryPoint>]
let main argv = 
    Application.Run exercise
    0
     
 
 

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 signature of the constructor is:

new : 
        driveName:string -> DriveInfo

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:

open System
open System.IO
open System.Drawing
open System.Windows.Forms

let exercise : Form = new Form(MaximizeBox = false, Text = "Exercise",
                               StartPosition = FormStartPosition.CenterScreen)

let diHardDrive : DriveInfo = new DriveInfo(@"C:\")

[<STAThread>]
[<EntryPoint>]
let main argv = 
    Application.Run exercise
    0

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:

member Name : string with get

Here is an example:

open System
open System.IO
open System.Drawing
open System.Windows.Forms

let exercise : Form = new Form(MaximizeBox = false, Text = "Exercise",
                               StartPosition = FormStartPosition.CenterScreen)

let lbxDrives : ListBox = new ListBox(Location = new Point(12, 12), Height = 130)

let drives : DriveInfo array = DriveInfo.GetDrives()

for drive in drives do
    lbxDrives.Items.Add drive.Name |> ignore
exercise.Controls.Add lbxDrives

[<STAThread>]
[<EntryPoint>]
let main argv = 
    Application.Run exercise
    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:

member DriveType : DriveType with 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:

member DriveFormat : string with get

Here is an example of accessing this property:

open System
open System.IO
open System.Drawing
open System.Windows.Forms

let exercise : Form = new Form(MaximizeBox = false, Text = "Exercise",
                               StartPosition = FormStartPosition.CenterScreen)

let lblOperatingSystem : Label = new Label(Location = new Point(12, 12), AutoSize = true)
exercise.Controls.Add lblOperatingSystem
let lblDriveFormat : Label = new Label(Location = new Point(12, 42), AutoSize = true)
exercise.Controls.Add lblDriveFormat

let diHardDrive : DriveInfo = new DriveInfo(@"C:\")

lblOperatingSystem.Text <- "Operating System:  " + Environment.OSVersion.ToString()
lblDriveFormat.Text     <- "Drive Format:      " + diHardDrive.DriveFormat

[<STAThread>]
[<EntryPoint>]
let main argv = 
    Application.Run exercise
    0

Run on Microsoft Windows 7 Ultimate, this may produce:

Drive Format

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:

member TotalSize : int64 with get

This property produces a long integer that represents the normal total capacity of a drive. Here is an example of accessing it:

open System
open System.IO
open System.Drawing
open System.Windows.Forms

let exercise : Form = new Form(MaximizeBox = false, Text = "Exercise",
                               StartPosition = FormStartPosition.CenterScreen)

let lblDriveCapacity : Label = new Label(Location = new Point(12, 12), AutoSize = true)
exercise.Controls.Add lblDriveCapacity

let diHardDrive : DriveInfo = new DriveInfo(@"C:\")

lblDriveCapacity.Text <- sprintf "Drive Capacity: %i bytes." diHardDrive.TotalSize

[<STAThread>]
[<EntryPoint>]
let main argv = 
    Application.Run exercise
    0

This would produce:

Drive Capacity

The Available Free Space of a Drive

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:

member AvailableFreeSpace : int64 with get

Here is an example of accessing this property:

open System
open System.IO
open System.Drawing
open System.Windows.Forms

let exercise : Form = new Form(MaximizeBox = false, Text = "Exercise",
                               StartPosition = FormStartPosition.CenterScreen)

let lblAvailableFreeSpace : Label = new Label(Location = new Point(12, 12), AutoSize = true)
exercise.Controls.Add lblAvailableFreeSpace

let diHardDrive : DriveInfo = new DriveInfo(@"C:\")

lblAvailableFreeSpace.Text <- sprintf "Available Free Space: %i bytes." diHardDrive.AvailableFreeSpace

[<STAThread>]
[<EntryPoint>]
let main argv = 
    Application.Run exercise
    0

This would produce:

The Available Free Space of a Drive

Directories

 

Introduction

A directory is a section of a medium (floppy disc, flash drive, hard drive, CD, DVD, etc) used to delimit a group of files. Because it is a "physical" area, it can handle operations not available on files. In fact, there are many fundamental differences between both:

  • A file is used to contain data. A directory doesn't contain data
  • A directory can contain one or more files and not vice-versa
  • A directory can contain other directories
  • A file can be moved from one directory to another. This operation is not possible vice-versa since a file cannot contain a directory

The similarities of both types are:

  • A directory or a file can be created. One of the restrictions is that two files cannot have the same name inside of the same directory. Two directories cannot have the same name inside of the same parent directory
  • A directory or a file can be renamed. If a directory is renamed, the "path" of its file(s) changes
  • A directory or a file can be deleted. If a directory is deleted, its files are deleted also
  • A directory or a file can be moved. If a directory moves, it "carries" all of its files to the new location
  • A directory or a file can be copied. A file can be copied from one directory to another. If a directory is copied to a new location, all of its files are also copied to the new location

Directory Creation

Before using a directory, you must first have it. You can use an existing directory if the operating system or someone else had already created one. You can also create a new directory. Directories are created and managed by various classes but the fundamental class is called Directory:

type Directory =  class end

Directory is a static class. All of its methods are static, which means you will never need to declare an instance of the Directory class in order to use it. Besides the Directory class, additional operations of folders and sub-folders can be performed using the DirectoryInfo class:

type DirectoryInfo =  
    class 
        inherit FileSystemInfo 
    end

To create a directory, you can call the CreateDirectory() method of the Directory class. This method is available in two versions. One of the versions uses the following signature:

static member CreateDirectory : 
        path:string -> DirectoryInfo

This method takes as argument the (complete) path of the desired directory. Here is an example:

E:\Programs\Business Orders\Customer Information

When this method is called:

  1. It first checks the parent drive, in this case E.
    If the drive doesn't exist, because this method cannot create a drive, the compiler would throw a DirectoryNotFoundException exception
  2. If the drive (in this case E) exists, the compiler moves to the first directory part of the path; in this case this would be the Programs folder in the E drive.
    If the folder doesn't exist, the compiler would create it. If that first directory doesn't exist, this means that the other directory(ies), if any, under the first don't exist. So, the compiler would create it/them
  3. If the first directory exists and if there is no other directory under that directory, the compiler would stop and would not do anything further
  4. If the directory exists and there is a sub-directory specified under it, the compiler would check the existence of that directory.
    If the sub-directory exists, the compiler would not do anything further and would stop.
    If the sub-directory doesn't exist, the compiler would create it
  5. The compiler would repeat step d until the end of the specified path

The Directory.CreateDirectory() method returns a DirectoryInfo object that you can use as you see fit.

Checking for a Directory Existence

Before using or creating a directory, you can first check if it exists. This is because, if a directory already exists in the location where you want to create it, you would be prevented from creating one with the same name. In the same way, if you just decide to directly use a directory that doesn't exist, the operation you want to perform may fail because the directory would not be found.

To check whether a directory exists or not, you can call the Directory.Exists() Boolean static method. Its signature is:

static member Exists : 
        path:string -> bool

This method receives the (complete) path of the directory. If the path exists, the method returns true. If the directory doesn't exist, the method returns false.

Deleting a Directory

To get rid of a directory, you can call the Delete() method of the Directory class. It is overloaded with two versions. One of the versions uses the following signature;

static member Delete : 
        path:string -> unit

When calling this method, pass the complete path as argument. The other version uses the following signature:

static member Delete : 
        path:string * 
        recursive:bool -> unit

This time, the second argument allows you to specifies whether you want the sub-folders and their contents to be deleted also.

Listing the Files of a Directory

One of the most routine operations performed in a directory consists of looking for a file. Microsoft Windows operating systems and the user's intuition have different ways of addressing this issue. The .NET Framework also provides its own means of performing this operation, through various techniques. You can start by checking the sub-directories and files inside of a main directory.

To look for files in a directory, the Directory class is equipped with a method named GetFiles:

static member GetFiles : 
        path:string -> string[] 

In the same way, the DirectoryInfo class can assist you with its GetFiles() method, which is overloaded with three versions:

member GetFiles : unit -> FileInfo[]
member GetFiles : 
        searchPattern:string -> FileInfo[] 
member GetFiles : 
        searchPattern:string * 
        searchOption:SearchOption -> FileInfo[]

 

   
   
 

Home Copyright © 2015 FunctionX Home