{ FTP example using WININET.PAS rather than an ACTIVEX control. Requires WININET.PAS
andWININET.DLL. WININET.DLL you can get from Microsoft, WININET.PAS is available
fromwww.borland.com, or with some versions ofDelphi 2.0.
You might Respond to OnNewDir events as follows:
procedure TForm1.FTP1NewDir(Sender: TObject);beginListBox1.Items :=
MyFtp1.FindFiles; // Get the directory listend; }
function GetFirstToken(S: string; Token: Char): string;varTemp: string;Index:
INteger;beginIndex := Pos(Token, S);if Index < 1 then beginGetFirstToken :=
'';Exit;end;Dec(Index);SetLength(Temp, Index);Move(S[1], Temp[1],
Index);GetFirstToken := Temp;end;
function StripFirstToken(S: string; Ch: Char): string;vari, Size: Integer;begini :=
Pos(Ch, S);if i = 0 then beginStripFirstToken := S;Exit;end;Size := (Length(S) - i);Move(S[i
+ 1], S[1], Size);SetLength(S, Size);StripFirstToken := S;end;
function ReverseStr(S: string): string;varLen: Integer;Temp: String;i,j:
Integer;beginLen := Length(S);SetLength(Temp, Len);j := Len;for i := 1 to Len do beginTemp[i]:=
S[j];dec(j);end;ReverseStr := Temp;end;
function GetDots(NumDots: Integer): string;varS: string;i: Integer;beginS := '';for
i := 1 to NumDots doS := S + ' ';Result := S;end;
function GetFindDataStr(FindData: TWin32FindData): string;varS: string;Temp:
string;begincase FindData.dwFileAttributes ofFILE_ATTRIBUTE_ARCHIVE: S := 'A';//
FILE_ATTRIBUTE_COMPRESSED: S := 'C';FILE_ATTRIBUTE_DIRECTORY: S :=
'D';FILE_ATTRIBUTE_HIDDEN: S := 'H';FILE_ATTRIBUTE_NORMAL: S :=
'N';FILE_ATTRIBUTE_READONLY: S := 'R';FILE_ATTRIBUTE_SYSTEM: S :=
'S';FILE_ATTRIBUTE_TEMPORARY: S := 'T';elseS := IntToStr(FindData.dwFileAttributes);end;S
:= S + GetDots(75);Move(FindData.CFilename[0], S[6],
StrLen(FindData.CFileName));Temp := IntToStr(FindData.nFileSizeLow);Move(Temp[1], S[25],
Length(Temp));Result := S;end;
function TMyFtp.BackOneDir: Boolean;varS: string;beginS := FCurDir;S :=
StripLastToken(S, '/');if S = '/' then beginResult := False;Exit;end;
if S <> '' then beginChangeDirExact(S);Result := True;end else
beginChangeDirExact('/');Result := True;end;
end;
// Changes to specific directory in Sfunction TMyFtp.ChangeDirExact(S: string):
Boolean;beginif S <> '' thenFtpSetCurrentDirectory(FFTPHandle, PChar(S));Result :=
True;FindFiles;SetUpNewDir;end;
// Assumes S has been returned by GetFindDataString;function
TMyFtp.ChangeDirCustom(S: string): Boolean;beginS := CustomToFileName(S);if S <> ''
thenFtpSetCurrentDirectory(FFTPHandle, PChar(S));Result := True;FindFiles;SetUpNewDir;end;
function GetFindDataStr(FindData: TWin32FindData): string;varS: string;Temp:
string;begincase FindData.dwFileAttributes ofFILE_ATTRIBUTE_ARCHIVE: S := 'A';//
FILE_ATTRIBUTE_COMPRESSED: S := 'C';FILE_ATTRIBUTE_DIRECTORY: S :=
'D';FILE_ATTRIBUTE_HIDDEN: S := 'H';FILE_ATTRIBUTE_NORMAL: S :=
'N';FILE_ATTRIBUTE_READONLY: S := 'R';FILE_ATTRIBUTE_SYSTEM: S :=
'S';FILE_ATTRIBUTE_TEMPORARY: S := 'T';elseS := IntToStr(FindData.dwFileAttributes);end;S
:= S + GetDots(75);Move(FindData.CFilename[0], S[6],
StrLen(FindData.CFileName));Temp := IntToStr(FindData.nFileSizeLow);Move(Temp[1], S[25],
Length(Temp));Result := S;end;
I'm not going to explain this process further in this newsletter. If you want more
information, you might look up FindFirst in the Delphi help. One final note: Unlike the
functions mentioned in the previous paragraph, TWin32FindData is not defined in
WININET.PAS, but instead can be found in the WIN32 help file that ships with Delphi. It is
declared in the WINDOWS.PAS file that ships with Delphi.我不准备在这里进一步解析这个进程。如果您想要更多的信息,您可以在Delphi帮助中查找FindFirst。最后提醒一句:并不向前文提及的函数,TWin32FindData并不是在WININET.PAS中定义的,但可以在随Delphi分发的WIN32帮助文件中找到它。它在随Delphi分发的WINDOWS.PAS文件中被定义。
function FtpGetFile(hFtpSession: HINTERNET; // Returned by
InternetConnectlpszRemoteFile: PChar; // File to getlpszNewFile: PChar; // Where to put it
on your PCfFailIfExists: BOOL; // Overwrite existing files?dwFlagsAndAttributes: DWORD; //
File attribute-See CreateFile.dwFlags: DWORD; // Binary or ASCII transferdwContext:
DWORD): // Usually zeroBOOL stdcall; // True on success
function InternetConnect(hInet: HINTERNET; // Handle from
InternetOpenlpszServerName: PChar; // Server: i.e., www.borland.comnServerPort:
INTERNET_PORT; // Usually 0lpszUsername: PChar; // usually anonymouslpszPassword: PChar;
// usually your email addressdwService: DWORD; // FTP, HTTP, or Gopher?dwFlags: DWORD; //
Usually 0dwContext: DWORD): // User defined number for callbackHINTERNET; stdcall;
function FtpGetCurrentDirectory(hFtpSession: HINTERNET; // handle from
InternetConnectlpszCurrentDirectory: PChar; // directory returned herevar
lpdwCurrentDirectory: DWORD): // buf size of 2nd parameterBOOL; stdcall; // True on
success