delphi实现映射和断开网络驱动器

Posted 游子日月长

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了delphi实现映射和断开网络驱动器相关的知识,希望对你有一定的参考价值。

type   TNetDiskMapper=class

  private
     FNetResource: TNetResource;
     FUserName,FPassWord:PWideChar;
  public
     constructor Create(DriveName,ShareURI,UserName,Password:PWideChar);
     destructor  Destory();
     function ConnectDiskMap:boolean;
     function disConnectDiskMap:boolean;
end;


var NetDiskMap: TNetDiskMapper;


{ TNetDiskMapper }

constructor TNetDiskMapper.Create(DriveName, ShareURI, UserName,
  Password: PWideChar);
begin
  FNetResource.dwType := RESOURCETYPE_DISK{磁盘资源};
  FNetResource.lpLocalName :=driveName { 指定本地设备 };
  FNetResource.lpRemoteName := PChar(ShareURI) { 指定远程网络名 };
  FNetResource.lpProvider := nil { 指定提供网络资源的供应商。如为空,则表示供应商未知。 };
  FUserName:=UserName { 远程资源的用户名 };
  FPassword:=Password { 远程资源的口令 };
end;

function TNetDiskMapper.ConnectDiskMap: boolean;
begin

{ WNetAddConnection2 的参数说明:

    dwFlags标志位用于指定登录时是否重新连接(0时表示不重新连接,CCONNECT_UPDATE_PROFILE登录时重新连接)。
}


   result:=false;
   case WNetAddConnection2(FNetResource,FPassword,FUserName,CONNECT_UPDATE_PROFILE) of
       NO_ERROR:begin result:=true; ShowMessage(映射成功) ; 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 is not valid or cannot be located.);
       ERROR_BAD_PROFILE :    ShowMessage(    The user profile is in an incorrect format.)   ;
       ERROR_CANNOT_OPEN_PROFILE :    ShowMessage( The system is unable to open the user profile to process persistent connections.    );
       ERROR_DEVICE_ALREADY_REMEMBERED    : ShowMessage(An entry for the device specified in lpLocalName is 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 is not started or the specified name cannot be used.);
       ERROR_NO_NETWORK:ShowMessage(    The network is not present. );
       else
       ShowMessage(其他意外终止!);
   end;
end;



destructor TNetDiskMapper.Destory;
begin
  disConnectDiskMap;
end;

function TNetDiskMapper.disConnectDiskMap: boolean;
begin
    if NO_ERROR= WNetCancelConnection2(FNetResource.lpLocalName,CONNECT_UPDATE_PROFILE,True) then
    begin
         result:=true;
    end
    else
       result:=false;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
//映射网络驱动器
    NetDiskMap:=TNetDiskMapper.Create(Z:,\\127.0.0.1\2016年安规调考,nil,nil);
    showmessage(booltostr(NetDiskMap.ConnectDiskMap,true));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
//断开网络驱动器

if assigned(NetDiskMap) and ( NetDiskMap<>nil) then showmessage(booltostr(NetDiskMap.disConnectDiskMap,true)); end;

 

以上是关于delphi实现映射和断开网络驱动器的主要内容,如果未能解决你的问题,请参考以下文章

如何实现映射的网络驱动器自动连接

无法断开网络驱动器.

如何断开网络驱动器,不让其自动加载

delphi 如何断开网络和连接网络

关于映射网络驱动器不成功的问题

关于映射网络驱动器时,用户名和密码输入错误的问题。