我找了一个用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盘设为共享目录,又怎么写呢?
谢谢啊!
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盘设为共享目录,又怎么写呢?
谢谢啊!
asender.currentdir := '/'; //设为当前目录
如果我写得程序放在了:D:\ftp,那么第一句的意思就是Asender.HomeDir:='d:\ftp\',可以这样理解吗?
第二句的意思就是把D:\ftp的文件列表发送给Ftp客户端。不知道我以上的理解对不对,呵呵
currentdir表示我们当前正处于默认目录下的哪个目录下,如:
Asender.HomeDir:='d:\ftp\';
ftp目录下还有01和02两个文件夹
当前目录可能是处于主目录下,也可能处于01或02目录下,就像"我的电脑"一样
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;
看到这个了吧,一个函数,功能是转换路径字符串
SlashToBackSlash负责把字符串里的'/'换成 '\'
看一下这句
FindFirst是查找电脑上的文件,也就是路径必须是\分隔符才行,例如D:\Delphi7就是\,所以必须把把'/'换成 '\'
那ListDirectory的功能就是把目录下的文件或文件夹放到ADirectoryListing里,发给Ftp客户端,对吗?
不好意思,又麻烦你了,呵呵