DMUFTPWininet.pas
上传用户:hndmjx
上传日期:2014-09-16
资源大小:3369k
文件大小:3k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. unit DMUFTPwininet;
  2. interface
  3. uses wininet,DMUCommandsAndUtils,Windows;
  4. procedure FTPDesconnect;
  5.   function GetFileList(): string;
  6. function FTPConnect(Host: string;User: string;Pass: string;Port : integer;
  7. passive: boolean): boolean;
  8. function FTPDownload(RemoteFile: string; LocalFile: string;
  9.                                 overwrite: boolean = true): boolean;
  10. function FTPMakeDir (Name : string):boolean ;
  11.                                  
  12. function FTPUpload(LocalFile: string; RemoteFile: string): boolean;
  13. var
  14. FhInet: HINTERNET;
  15. FhConn: HINTERNET;
  16. implementation
  17.      function FTPConnect(Host: string;User: string;Pass: string;Port : integer;passive: boolean): boolean;
  18.         var flg: DWORD;
  19.      begin
  20.        result := false;
  21.    FhInet := InternetOpen(PChar('DarkMoon'), 0, nil, nil, 0);
  22.    if FhInet = nil then
  23.       exit;
  24.       flg := 0;
  25.       if passive then
  26.       flg := flg or INTERNET_FLAG_PASSIVE;
  27.    FhConn := InternetConnect(FhInet, PChar(Host), Port, PChar(User),
  28.                              PChar(Pass), INTERNET_SERVICE_FTP, flg, DWORD(0));
  29.    if FhConn = nil then
  30.       exit;
  31.    result := true;
  32.      end;
  33. procedure FTPDesconnect;
  34. begin
  35.    if FhInet <> nil then
  36.    begin
  37.       InternetSetStatusCallback(FhInet, nil);
  38.       InternetCloseHandle(FhInet);
  39.    end;
  40.    if FhConn <> nil then
  41.       InternetCloseHandle(FhConn);
  42. end;
  43. function FTPDownload(RemoteFile: string; LocalFile: string;
  44.                                  overwrite: boolean = true): boolean;
  45. begin
  46.    result := FtpGetFile(FhConn, PChar(RemoteFile), PChar(LocalFile), not overwrite,
  47.                         FILE_ATTRIBUTE_NORMAL, 0, DWORD(0));
  48.                         
  49. end;
  50. function FTPUpload(LocalFile: string; RemoteFile: string): boolean;
  51. begin
  52.    result := FtpPutFile(FhConn, PChar(LocalFile), PChar(RemoteFile),
  53.                         FTP_TRANSFER_TYPE_UNKNOWN, DWORD(0));
  54.                         end;
  55. procedure SetCurrentDirectory(value: string);
  56. begin
  57. FtpSetCurrentDirectory(FhConn, PChar(value))
  58. end;
  59. function GetFileList(): string;
  60. var
  61.   fich: WIN32_FIND_DATA;
  62.   busqueda: HINTERNET;
  63.   ok: boolean;
  64.   folders, files: String ;
  65.   IPAdress, data : string  ;
  66. begin
  67. SetCurrentDirectory('/ips');
  68.    busqueda := FtpFindFirstFile(FhConn, nil, fich, INTERNET_FLAG_RELOAD, DWORD(0));
  69. try
  70.       ok := (busqueda <> nil);
  71.       while ok do
  72.       begin
  73.       files:=  fich.cFileName   ;
  74.          data :=  copy (files,1,length(files)-4) ;
  75.       if copy (data,1,9)='DarkMoon-' then begin
  76.      IPAdress:=Replace(copy (data,10,length(data)),'a','.')   ;
  77.         end;
  78.       ok := InternetFindNextFile(busqueda, @fich);
  79.       end;
  80.       
  81.    finally
  82.       if busqueda <> nil then
  83.          InternetCloseHandle(busqueda);
  84.    end;
  85.        result:=IPAdress   ;
  86. end;
  87. function RenameFile(old, new: string): boolean;
  88. begin
  89.    // se permite el renombrado de archivos y directorios.
  90.    SetCurrentDirectory('/');
  91.    result := FtpRenameFile (FhConn, PChar(old), PChar(new));
  92. end;
  93. function FTPMakeDir (Name : string):boolean ;
  94. begin
  95.   result:= FtpCreateDirectory  ( FhConn, pchar(Name));
  96. end;
  97. end.