• 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.12016年安规调考',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;
  • 相关阅读:
    [题解] uva 247 Calling Circles (floyd判联通/tarjan强连通分量)
    [总结] 最小生成树 minimum spanning tree
    [题解] uva 11865 Stream My Contest (二分+最小树形图)
    [题解] poj 3164 Command Network (朱刘算法 最小树形图(有向生成树))
    [题解] poj 3241 Object Clustering (kruskal曼哈顿距离最小生成树+树状数组)
    [BZOJ2301][HAOI2011]Problem b 莫比乌斯反演+容斥
    [BZOJ1101][POI2007]Zap 莫比乌斯反演
    [BZOJ4006][JLOI2015]管道连接 状压dp+斯坦纳树
    [BZOJ1494][NOI2007]生成树计数 状压dp 并查集
    [BZOJ1076][SCOI2008]奖励关 状压dp
  • 原文地址:https://www.cnblogs.com/yzryc/p/6292655.html
Copyright © 2020-2023  润新知