home *** CD-ROM | disk | FTP | other *** search
- unit MainForm;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, Buttons, ComCtrls, WinINet, ExtCtrls;
-
- type
- TForm1 = class(TForm)
- FileList: TListView;
- GetFileListButton: TButton;
- CurrentDir: TLabel;
- Panel1: TPanel;
- Status: TLabel;
- UpButton: TButton;
- procedure GetFileListButtonClick(Sender: TObject);
- procedure FileListDblClick(Sender: TObject);
- procedure UpButtonClick(Sender: TObject);
- procedure FileListCompare(Sender: TObject; Item1, Item2: TListItem;
- Data: Integer; var Compare: Integer);
- private
- { Private declarations }
- hSession: HInternet;
- hFTP: HInternet;
- hFind: HInternet;
- ThisDir: String;
- procedure GetFileList (const Dir: String);
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- procedure MyCallBack (hHandle: HINTERNET; Self: TForm1; dwStatus: DWord; pStatus: Pointer; dwLen: DWord); stdcall;
- var
- ErrNum, BuffSize: DWord;
- szBuff: array [0..1024] of Char;
- begin
- BuffSize := sizeof (szBuff);
- if InternetGetLastResponseInfo (ErrNum, szBuff, BuffSize) then
- if (BuffSize > 0) and (szBuff [0] <> #0) then
- Self.Status.Caption := szBuff;
- end;
-
- procedure TForm1.GetFileList (const Dir: String);
- var
- Item: TListItem;
- szDirSize: Cardinal;
- FileData: TWin32FindData;
- szDirectory: array [0..512] of Char;
- begin
- Screen.Cursor := crHourGlass;
- try
- hFTP := InternetConnect (hSession, 'ftp.microsoft.com',
- Internet_Default_FTP_Port, Nil, Nil,
- Internet_Service_FTP, 0, Cardinal (Self));
- if hFTP <> Nil then try
-
- InternetSetStatusCallback (hFTP, @MyCallback);
- Status.Caption := 'Connected to ftp.microsoft.com';
-
- if Dir <> '' then FtpSetCurrentDirectory (hFTP, PChar (Dir));
-
- // Get current directory
- szDirSize := sizeof (szDirectory);
- if FtpGetCurrentDirectory (hFTP, szDirectory, szDirSize) then begin
- ThisDir := szDirectory;
- CurrentDir.Caption := 'Current Directory = ' + szDirectory;
- end;
-
- // The main enumeration loop
- FileList.Items.Clear;
- hFind := FtpFindFirstFile (hFTP, '*.*', FileData, 0, Cardinal (Self));
- if hFind <> Nil then try
- while True do begin
- if GetLastError = Error_No_More_Files then break;
- // We've got a file
- Item := FileList.Items.Add;
- Item.Caption := FileData.cFileName;
- // Is this a directory or a file?
- if FileData.dwFileAttributes = File_Attribute_Directory then begin
- Item.Data := Pointer (1);
- Item.SubItems.Add ('--dir--');
- end else begin
- Item.Data := Nil;
- Item.SubItems.Add (IntToStr (FileData.nFileSizeLow));
- end;
-
- if not InternetFindNextFile (hFind, @FileData) then break;
- end;
- finally
- InternetCloseHandle (hFind);
- end;
- finally
- InternetCloseHandle (hFTP);
- end;
- finally
- Screen.Cursor := crDefault;
- end;
- end;
-
- procedure TForm1.GetFileListButtonClick (Sender: TObject);
- begin
- hSession := InternetOpen ('DelphiMagWinINetDemo', Internet_Open_Type_PreConfig, Nil, Nil, 0);
- if hSession <> Nil then try
- GetFileList (ThisDir);
- finally
- InternetCloseHandle (hSession);
- end;
- end;
-
- procedure TForm1.FileListDblClick(Sender: TObject);
- var
- Item: TListItem;
- NewDir: String;
- begin
- // Is this a directory double-click?
- Item := FileList.Selected;
- if (Item <> Nil) and (Item.Data <> Nil) then begin
- NewDir := ThisDir;
- if NewDir = '' then NewDir := '/';
- if NewDir [Length (NewDir)] <> '/' then NewDir := NewDir + '/';
- ThisDir := NewDir + Item.Caption;
- GetFileListButtonClick (Sender);
- end;
- end;
-
- procedure TForm1.UpButtonClick(Sender: TObject);
- var
- p: PChar;
- NewDir: array [0..512] of Char;
- begin
- if (ThisDir <> '') and (ThisDir <> '/') then begin
- StrPCopy (NewDir, ThisDir);
- p := StrRScan (NewDir, '/');
- if p <> Nil then begin
- p^ := #0;
- ThisDir := NewDir;
- if ThisDir = '' then ThisDir := '/';
- GetFileListButtonClick (Sender);
- end;
- end;
- end;
-
- procedure TForm1.FileListCompare(Sender: TObject; Item1, Item2: TListItem; Data: Integer; var Compare: Integer);
- begin
- Compare := Ord (Ord (Item1.Data <> Nil) < Ord (Item2.Data <> Nil));
- end;
-
- end.
-
-