chami.com/tips/
Last  Home  Next
 Internet
 Programming
 Windows


Click for details
Keywords
Delphi 2.x
Delphi
Functions
Win32

Function to list all the network drive mappings

    See Also
  Explore the Explorer (Windows tip)
  Make items inside Control Panel, Printers, and Dial-Up Networking Folder accessible directly from the Start Menu (Windows tip)
  Quick visit to your network neighborhood (Windows tip)
  Why connect if you don't have to? (Windows tip)

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;

If you need to programmatically map and delete network drives, look up WNetAddConnection(), WNetAddConnection2(), WNetAddConnection3(), WNetCancelConnection(), and WNetCancelConnection2() in your "Win32 Programmer's Reference."

 
Related Links Email Print 
Created on 28-Nov-1996. Source code colorized using CodeColorizer.
Copyright (C) 1996-99 Chami.com All Rights Reserved. Reproduction in whole or in part
or in any form or medium without express written permission of Chami.com is prohibited.
Information on this page is provided as-is without warranty of any kind. Use at your own risk.
Free Downloads | Products & Services | Privacy Statement | Terms & Conditions | Advertising Info