Mega Code Archive

 
Categories / Delphi / System
 

Get all drives on a system without a TDriveComboBox

Title: Get all drives on a system without a TDriveComboBox Question: This code shows you how to get all logical drives on a system without having to add a DriveComboBox to your application. Answer: //This code returns a list of logical drives on any system //Use this code for quicker results than iterating through //a DriveComboBox. function GetListOfLogicalDrives: TStringList; var lpDrives: array [0..500] of char; lpStrTmp: string; iiDCount, iiResult: integer; begin Result := TStringList.Create; lpStrTmp := ''; iiResult := GetLogicalDriveStrings(500, lpDrives); for iiDCount := 0 to iiResult do begin if lpDrives[iiDCount] #0 then begin lpStrTmp := lpStrTmp + lpDrives[iiDCount]; if Length(lpStrTmp) = 3 then begin Result.Add(lpStrTmp); lpStrTmp := ''; end; end; end; end; procedure TForm1.Button1Click(Sender: TObject); begin ListBox1.Items := GetListOfLogicalDrives; //Puts drives in listbox// end; //If you wanted to get each drive's icon(s) (to add to a TListView) //you can use the SHGetFileInfo api call. (in shellapi.pas) //If you wanted to filter certain types of drives you can use the //GetDriveType api call. //Both of these API's are well documented in Delphi, as well as on //the MSDN (msdn.microsoft.com)