How to check and create drive mapping under NT with code

Well...

I would like to do 2 things.

I want to be able to find out what drives a machine currently has mapped.

then how to map a drive.... All from with in delphi code.
bohnetAsked:
Who is Participating?
 
intheConnect With a Mentor Commented:
and the getting list of drives:
taken from tip on www.chami.com

It is very easy to get a list of all the network drive mappings using the following function. Please note that you must create and free the string list that you pass to it. The return value indicates the number of network mappings GetNetworkDriveMappings() was able to find.

function GetNetworkDriveMappings(
  sl : TStrings ) : integer;
var
  i               : integer;
  sNetPath        : string;
  dwMaxNetPathLen : DWord;
begin
  sl.Clear;
  dwMaxNetPathLen := MAX_PATH;
  SetLength( sNetPath,
    dwMaxNetPathLen );
  for i := 0 to 25 do
  begin
    if( NO_ERROR =
      Windows.WNetGetConnection(
        PChar(
          '' + Chr( 65 + i ) + ':' ),
        PChar( sNetPath ),
        dwMaxNetPathLen ) )then
    begin
      sl.Add( Chr( 65 + i ) + ': ' +
              sNetPath );
    end;
  end;
  Result := sl.Count;
end;

//
// here's how to call GetNetworkDriveMappings():
//
var
  sl : TStrings;
  nMappingsCount,
  i  : integer;
begin
  sl := TStringList.Create;
  nMappingsCount :=
    GetNetworkDriveMappings( sl );
  for i := 0 to nMappingsCount-1 do
  begin
    //
    // do your thing here...
    // for now, we'll just display the mapping
    //
    MessageBox( 0,
      PChar( sl.Strings[ i ] ),
      'Network drive mappings',
      MB_OK );
  end;
  sl.Free;
end;
 


Regards Barry

0
 
intheCommented:
well here is the mapping part:
to briing up the drive mapping dialog box do:

procedure TForm1.Button2Click(Sender: TObject);
begin
  WNetConnectionDialog(Handle,RESOURCETYPE_DISK);
   {RESOURCETYPE_PRINT for printer}
end;


or to map the drive yourself something like:

procedure TForm1.Button1Click(Sender: TObject);
var
 NetResource: TNetResource;
  begin
  { fill our TNetResource record structure }
 NetResource.dwType       := RESOURCETYPE_DISK;
 NetResource.lpLocalName  := 'S:';
 NetResource.lpRemoteName := '\\server\public';
 NetResource.lpProvider   := '';
{ map our network drive using our TNetResource
 record structure }
try
 WNetAddConnection2(NetResource,
      '', {Password or empty}
      '', {User name }
 CONNECT_UPDATE_PROFILE);
 finally
 exception.Create('drive not found');
 end;
   end;{In that example, we would connect '\\server\public' to S:,
 without username or password }

Regards Barry
0
 
bohnetAuthor Commented:
Thank You
0
All Courses

From novice to tech pro — start learning today.