Mega Code Archive

 
Categories / Delphi / LAN Web TCP
 

Mapingunmaping virtual drives

Title: maping/unmaping virtual drives Question: Have you ever got tired of clicking in Explorer for that file that's in the deepest tree-node?. at least: c:\program files\borland\delphi5\projects\... Answer: I've created two functions that will let you create Virtual Drives for your most commonly used folders (local hard drive folders like c:\program files\borland\delphi5\projects or even network folders, like f:\myapplications\utils), so it will appear on "My Computer" as a new drive and you can access it very quickly. The elder Delphians-programmers will remember the good old DOS-days where we had a command called SUBST... well... we still have it! here are the functions: Procedure UnmapVirtualDrive(Const Drive:Char); Begin If (GetDriveType(PChar(Drive+':\'))&ltDRIVE_FIXED) Then Raise Exception.Create('['+Drive+':\] Is not a Virtual drive'); WinExec(PChar('subst /d '+Drive+': '), SW_HIDE); Sleep(250); //give some time for the execution If (GetDriveType(PChar(Drive+':\'))1) Then Raise Exception.Create('Could not unmap Virtual drive ['+Drive+':\]') End; Function MapVirtualDrive(Const Drive:Char; Const Path:String):Boolean; Var DType:Integer; Begin DType:=GetDriveType(PChar(Drive+':\')); If (DType=DRIVE_FIXED) Then Begin If (MessageDlg('Drive ['+Drive+':\] already exists, Do you want to try And replace it?', mtWarning, [mbYes, mbNo, mbCancel], 0)=mrYes) Then UnmapVirtualDrive(Drive) End Else If (DType1) Then Raise Exception.Create('Drive '+Drive+':\ cannot be maped (drive not overwritable)'); WinExec(PChar('subst '+Drive+': '+Path), SW_HIDE); Sleep(250); //give some time for the execution Application.ProcessMessages; Result:=GetDriveType(PChar(Drive+':\'))=DRIVE_FIXED End; - the MapVirtualDrive function accepts a Char, which will be the new drive, and the path to where it will point to... it returns true if succesful - the UnmapVirtualDrive also accepts a Char, which will be the drive you need to unmap; the procedure tries to unmap the drive and returns an exception if any error happens ok... now you want an example... let's see... drop two TEdits on your form and two TButtons on the first Edit put the letter you want for your new drive, and in the Edit2 put the path to where it will point: on the onclick of the first button put this: Procedure TForm1.Button1Click(Sender: TObject); Begin If (MapVirtualDrive(Edit1.Text[1], Edit2.Text)) Then ShowMessage('Virtual Drive ['+Edit1.Text+'] created') Else ShowMessage('Could not map Virtual Drive ['+Edit1.Text+']') End; on the onclick of the second button put this: Procedure TForm1.Button3Click(Sender: TObject); Begin UnmapVirtualDrive(Edit1.Text[1]); ShowMessage(Edit1.Text+' unmaped succesful') End; and that's it... you have a button to map, and the other to unmap... you can open "my computer" and see your new drives there... or... here's procedure that will show all your drives in a Memo: Procedure ListDrives(DrivesStrs:TStrings); Var Drive: Char; DriveLetter: String[4]; sDriveType: String; Begin For Drive := 'A' To 'Z' Do Begin DriveLetter := Drive + ':\'; sDriveType := ''; Case GetDriveType(PChar(Drive + ':\')) Of DRIVE_REMOVABLE: sDriveType := 'Floppy'; DRIVE_FIXED: sDriveType := 'Fixed'; DRIVE_REMOTE: sDriveType := 'Network'; DRIVE_CDROM: sDriveType := 'CD-ROM'; DRIVE_RAMDISK: sDriveType := 'RAM Disk'; End; If sDriveType &lt '' Then Begin sDriveType := sDriveType + ' Drive '; DrivesStrs.Add(Format('Drive %s:', [Drive])+' - '+sDriveType); End; End; End; and you could call it just like: ListDrives(MyMemo.Lines); or... you know, any component that uses TStrings I hope you find these functions useful The source code of the article was formatted using my: PAS 2 HTML converter keep up coding! salu2 EberSys