Mega Code Archive

 
Categories / Delphi / LAN Web TCP
 

Connect to a networkdrive

Title: Connect to a networkdrive Question: How to mount a networkdrive or work witht he UNC path ! Answer: The functions 'WNetAddConnection2' mounts a networkdrive. Is 'aDriveLetter' empty, there will be no driveletter found and the windows explorer doesn't show the connection. In this case you have to work with the UNC path. function ConnectDrive(aUser,aPwd,aUncPath,aDriveLetter: string; var aErrorCode: integer): boolean; var ANetResource: TNetResource; PW,UN: array[0..200] of char; Error: word; begin aUser := aUser + #0; aPwd := aPwd + #0; aUncPath := aUncPath + #0; with ANetResource do begin dwType:=RESOURCETYPE_DISK; lpLocalName := PChar(aDriveLetter); lpRemoteName := PChar(aUncPath); lpProvider:=nil; end; StrCopy(PW,@aPwd[1]); StrCopy(UN,@aUser[1]); Error := WNetAddConnection2(ANetResource, @PW, @UN, 0); aErrorCode := Error; Result := (Error = 0); end;