typeTnetdiskmapper=class PrivateFnetresource:tnetresource; Fusername,fpassword:pwidechar; Public Constructor Create(Drivename,shareuri,username,password:pwidechar); destructordestory (); functionConnectdiskmap:boolean; functionDisconnectdiskmap:boolean;End;varNetdiskmap:tnetdiskmapper;{Tnetdiskmapper}ConstructorTnetdiskmapper.Create(DriveName, Shareuri, UserName, Password:pwidechar);beginFnetresource.dwtype:=resourcetype_disk{disk resource}; Fnetresource.lplocalname:=drivename {specify local device}; Fnetresource.lpremotename:=PChar (Shareuri) {Specifies the remote network name}; Fnetresource.lpprovider:=Nil {Specifies the vendor that provides network resources. If empty, the vendor is unknown. }; Fusername:=UserName {User name of remote resource}; Fpassword:=Password {password for remote resource};End;functionTNetDiskMapper.ConnectDiskMap:boolean;begin
{WNetAddConnection2 's parameter description:
The dwflags flag bit is used to specify whether to reconnect at logon (0 indicates no reconnection, Cconnect_update_ re-connect when profile is logged on).
}
Result:=false; CaseWNetAddConnection2 (Fnetresource,fpassword,fusername,connect_update_profile) ofNo_error:beginResult:=true; ShowMessage ('Mapping succeeded') ;End; Error_access_denied:showmessage ('Access is denied.'); Error_already_assigned:showmessage ('The device specified in the lpLocalName parameter is already connected.'); Error_bad_dev_type:showmessage ('The device type and the resource type do not match.'); Error_bad_device:showmessage ('The value specified in lpLocalName is invalid'); Error_bad_net_name:showmessage ('The value specified in the Lpremotename parameter are not valid or cannot be located.'); Error_bad_profile:showmessage ('The user profile is a incorrect format.') ; Error_cannot_open_profile:showmessage ('The system is unable to open the user profiles to process persistent connections. '); Error_device_already_remembered:showmessage ('An entry for the device specified in lpLocalName are already in the user profile.') ; Error_extended_error:showmessage ('A network-specific error occurred. To get a description of the error, use the Wnetgetlasterror function. '); Error_invalid_password:showmessage ('The specified password is invalid.'); Error_no_net_or_bad_path:showmessage ('The operation cannot be performed because either a network component are not started or the specified name cannot be u Sed.'); Error_no_network:showmessage ('The network is not present.'); ElseShowMessage ('other unexpected termination!'); End;End;destructortnetdiskmapper.destory;beginDisconnectdiskmap;End;functionTNetDiskMapper.disConnectDiskMap:boolean;begin ifNo_error= WNetCancelConnection2 (fnetresource.lplocalname,connect_update_profile,true) Then beginResult:=true; End ElseResult:=false;End;procedureTform1.button1click (sender:tobject);begin//Map Network DrivesNetdiskmap:=tnetdiskmapper.Create('Z:','\\127.0.0.1\2016 Annual Safety regulation',Nil,Nil); ShowMessage (Booltostr (netdiskmap.connectdiskmap,true));End;procedureTform1.button2click (sender:tobject);begin//Disconnecting a network drive
If assigned (NETDISKMAP) and (Netdiskmap<>nil) thenShowMessage (Booltostr (netdiskmap.disconnectdiskmap,true));End;
Delphi implements mapping and disconnecting network drives