» Help

How to create a virtual drive

   

This is a very simple method to create a new drive which links to a folder, for example: let's say we want a new drive letter (W) which points to our Delphi components folder (D:\Delphi\Components\). Create a new VCL application, drop 2 buttons on the form, change caption of Button1 to CreateDrive and the caption of Button2 to RemoveDrive, paste the fallowing 3 methods after the implementation keyword.

function SystemDir: string;
begin
  (* get system32 folder *)
  SetLength(Result, MAX_PATH);
  GetSystemDirectory(@Result[1], MAX_PATH);
end;
 
procedure DriveLinkCreate(const Drive: Char; const Path: String);
var
  Param: String;
begin
  (* format the call parameter *)
  Param := Format('%s: "%s"', [Drive, Path]);
  (* and bang! we get a new drive *)
  ShellExecute(1, 'open', 'subst', PChar(Param),
    PChar(SystemDir), 0);
end;
 
procedure DriveLinkRemove(const Drive: Char);
var
  Param: String;
begin
  (* format the call parameter with the /d option
     which stands for delete *)
  Param := Format('%s: /d', [Drive]);
  (* now we remove the virtual drive *)
  ShellExecute(1, 'open', 'subst', PChar(Param),
    PChar(SystemDir), 0);
end;

Now press F12 to see the form, double-click the CreateDrive button and paste the following code:

  DriveLinkCreate('W', 'D:\Delphi\Components');

Replace D:\Delphi\Components with your delphi component folder, press F12 again and double-click the RemoveDrive button and paste this code:

  DriveLinkRemove('W');

Don't forget to add ShellApi to uses clause.

Author: Dorin Duminica
Contributor: topellina
Added: 2013-09-06
Last updated: 2013-09-06

« Return to contents »