我找了一个用IdFtpServer做Ftp服务器的例子,UserLogin事件是这么写的:
procedure TFTPServer.IdFTPServer1UserLogin( ASender: TIdFTPServerThread;
  const AUsername, APassword: string; var AAuthenticated: Boolean ) ;
begin
  AAuthenticated := ( AUsername = '123' ) and ( APassword = '123' ) ;
  if not AAuthenticated then
    exit;
  ASender.HomeDir := '/';//设为根目录?
  asender.currentdir := '/';
end;设置目录那两句我看不懂,请大家指点下。如果在Windows下运行这个程序,请问那两句把哪个目录设为主目录?如果我想把C盘设为共享目录,又怎么写呢?
谢谢啊!

解决方案 »

  1.   

    ASender.HomeDir := '/';//设为根目录? /表示当前程序所在的目录
      asender.currentdir := '/'; //设为当前目录
      

  2.   

    那可不可以这么说:
    如果我写得程序放在了:D:\ftp,那么第一句的意思就是Asender.HomeDir:='d:\ftp\',可以这样理解吗?
    第二句的意思就是把D:\ftp的文件列表发送给Ftp客户端。不知道我以上的理解对不对,呵呵
      

  3.   

    HomeDir相当于我们平常所说的默认目录
    currentdir表示我们当前正处于默认目录下的哪个目录下,如:
    Asender.HomeDir:='d:\ftp\';
    ftp目录下还有01和02两个文件夹
    当前目录可能是处于主目录下,也可能处于01或02目录下,就像"我的电脑"一样
      

  4.   

    再请教你个问题:就是下面有注释的那句话的TransLatePath是做什么用的?是不是Windows的目录和Ftp的目录不一样?就像Windows的目录中用“\”,而Linux用“/”?当我把下面的代码贴到我的程序中时,Ftp客户端列不出目录,而当我把“TransLatePath( apath, ASender.HomeDir ) + '*.*'改成“'e:\'+ '*.*'”时,就能在客户端列出e盘下的文件。这是为什么?procedure TFTPServer.IdFTPServer1ListDirectory( ASender: TIdFTPServerThread; const APath: string; ADirectoryListing: TIdFTPListItems ) ;  procedure AddlistItem( aDirectoryListing: TIdFTPListItems; Filename: string; ItemType: TIdDirItemType; size: int64; date: tdatetime ) ;
      var
        listitem: TIdFTPListItem;
      begin
        listitem := aDirectoryListing.Add;
        listitem.ItemType := ItemType;
        listitem.FileName := Filename;
        listitem.OwnerName := 'anonymous';
        listitem.GroupName := 'all';
        listitem.OwnerPermissions := '---';
        listitem.GroupPermissions := '---';
        listitem.UserPermissions := '---';
        listitem.Size := size;
        listitem.ModifiedDate := date;
      end;var
      f: tsearchrec;
      a: integer;
    begin
      ADirectoryListing.DirectoryName := apath;
      //ShowMessage('现在使用的目录是: '+APath);
      a := FindFirst( TransLatePath( apath, ASender.HomeDir ) + '*.*', faAnyFile, f ) ;//这句的TransLatePath是什么意思?
      while ( a = 0 ) do
      begin
        if ( f.Attr and faDirectory > 0 ) then
          AddlistItem( ADirectoryListing, f.Name, ditDirectory, f.size, FileDateToDateTime( f.Time ) )
        else
          AddlistItem( ADirectoryListing, f.Name, ditFile, f.size, FileDateToDateTime( f.Time ) ) ;
        a := FindNext( f ) ;
      end;  FindClose( f ) ;
    end;
    function TFTPServer.TransLatePath( const APathname, homeDir: string ) : string;
    var
      tmppath: string;
    begin
      result := SlashToBackSlash( homeDir ) ;
      tmppath := SlashToBackSlash( APathname ) ;
      if homedir = '/' then
      begin
        result := tmppath;
        exit;
      end;  if length( APathname ) = 0 then
        exit;
      if result[length( result ) ] = '\' then
        result := copy( result, 1, length( result ) - 1 ) ;
      if tmppath[1] <> '\' then
        result := result + '\';
      result := result + tmppath;
    end;function SlashToBackSlash( const str: string ) : string;
    var
      a: dword;
    begin
      result := str;
      for a := 1 to length( result ) do
        if result[a] = '/' then
          result[a] := '\';
    end;
      

  5.   

    function TFTPServer.TransLatePath( const APathname, homeDir: string ) : string; 
    看到这个了吧,一个函数,功能是转换路径字符串
    SlashToBackSlash负责把字符串里的'/'换成 '\'
      

  6.   

    FindFirst( TransLatePath( apath, ASender.HomeDir ) + '*.*', faAnyFile, f ) ;
    看一下这句
    FindFirst是查找电脑上的文件,也就是路径必须是\分隔符才行,例如D:\Delphi7就是\,所以必须把把'/'换成 '\'
      

  7.   

    那就是说APath中的路径都是用地“/”来分割的,
    那ListDirectory的功能就是把目录下的文件或文件夹放到ADirectoryListing里,发给Ftp客户端,对吗?
    不好意思,又麻烦你了,呵呵
      

  8.   

    IdFTPServer1ListDirectory列举FTP服务器目录事件