delphi6或7 Idftp.DirectoryListing 返回内容为空解决方法

delphi6或7 Idftp.DirectoryListing 返回内容为空解决方法Idftp.DirectoryListing.Count位0最后发现由于D6/D7以后的版本的intra控件升级,管理方式变了,需要在调用的单元是加

delphi6或7 Idftp.DirectoryListing 返回内容为空解决方法

Idftp.DirectoryListing.Count位0



最后发现由于D6/D7以后的版本的intra控件升级,管理方式变了,需要在调用的单元是加一个文件IdAllFTPListParsers才搞定。现在贴出来,让大家少走点弯路。

原来是Indy10的一个设计上的变化,以下引用官网内容:

TIdFTP.DirectoryListing is returning nothing for my FTP server. Why?

We restructured the way FTP directory listing is done. The code is now a plug in system with various parsers in their own units which register themselves into a framework. To add support for a particular type of FTP server, you simply add the parser unit to one of your program unit’s uses clause.

To add the Unix directory list parser, add IdFTPListParseUnix to your unit’s clause. To add support for Microsoft Windows NT IIS FTP server, add IdFTPListParseWindowsNT to your uses clause. You can also add many other parsers we include in the Indy packages. By convention, they are named IdFTPListParser followed by the server type. You do not need to add IdFTPListParseBase to your uses clause because IdFTP uses that unit. If you wish to use all of Indy’s FTP List parser classes, just add the IdAllFTPListParsers unit to your uses clause.

We admit this new design is not as intuitive as the older one but this has many advantages for us such as:

• The FTP list parsing code is now much easier to maintain than the same code in a non-modular design in one unit which would be at least 150KB total.

• You now can add support for only the FTP servers you want your program support to support.

• You can now write your own custom parser classes and plug those into the standard FTP parsing list framework.


如在uses里面加多IdAllFTPListParsers 单元

演示代码:


interface


uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, IdAntiFreezeBase, IdAntiFreeze, IdBaseComponent, IdComponent,

  IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase, IdFTP, IdFTPCommon,

  IdFTPList, IdAllFTPListParsers,

  ExtCtrls, ComCtrls, StdCtrls, Buttons;


procedure TfrmFtpTest.ChangeDir(DirName: string);

var

  LS: TStringList;

  i, n: Integer;

  DL: TIdFTPListItems;

  li: TIdFTPListItem;

  s: string;

begin

  LS:=TStringList.Create;

  try

    ftp.ChangeDir(DirName);

    ftp.TransferType:=ftBinary;//ftASCII;


    //或  //注意: 需要在调用的单元是加一个文件IdAllFTPListParsers才能正确返回结果

   ftp.List(nil);

    ftp.DirectoryListing;

    n:=ftp.DirectoryListing.Count;

    for i:=0 to n - 1 do

    begin

      Application.ProcessMessages;

      li:=ftp.DirectoryListing.Items[i]; //得到当前目录下文件及目录列表

      s:=Format('%s %s', [IdDirItemTypeStr(li.ItemType), li.FileName]);

      MLog(s);

    end;

  finally

    LS.Free;

  end;

end;


这样就可以正常返回数据了

image.png

相关推荐

[!--temp.pl--]