游子日月长

笑渐不闻声渐悄,多情却被无情恼!

导航

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;

 

posted on 2017-01-17 13:17  游子日月长  阅读(570)  评论(0编辑  收藏  举报