How to Connect to a Network
Drive in Delphi
This document explains how to create a 'Network' button that
brings up a connection dialog and then sets a drive box to point
to the new drive in Delphi. The code was created in Delphi 2,
but doing it in Delphi 1 should be about the same procedure.
Create a command button named NetBtn, and a drive combo box
named DriveBox. Then type this code in the OnClick event for the
button:
procedure TStartForm.NetBtnClick(Sender: TObject);
var
OldDrives: TStringList;
i: Integer;
begin
OldDrives := TStringList.Create;
OldDrives.Assign(Drivebox.Items); // Remember old drive list
// Show the connection dialog
if WNetConnectionDialog(Handle, RESOURCETYPE_DISK) = NO_ERROR then begin
DriveBox.TextCase := tcLowerCase; // Refresh the drive list box
for i := 0 to DriveBox.Items.Count - 1 do begin
if Olddrives.IndexOf(Drivebox.Items) = -1 then begin // Find new Drive letter
DriveBox.ItemIndex := i; // Updates the drive list box to new drive letter
DriveBox.Drive := DriveBox.Text[1]; // Cascades the update to connected directory lists, etc
end;
end;
DriveBox.SetFocus;
end;
OldDrives.Free;
end;