If your program relies on Packages or DLLs than deploying new versions of your libraries certainly takes some time. The Internet provides a fairly easy way to accomplish this task. Adding an auto-update option to your applications could be the best way to keep your Delphi applications up to date.
Let's see how to create the most important piece of code in any FTP application.
Delphi gives us full access to the WinInet API (wininet.pas) which we can use to connect to and retrieve files from any Web site that uses either Hypertext Transfer Protocol (HTTP) or File Transfer Protocol (FTP). For example, we could use the functions inside the WinInet API to: add an FTP browser to any application, create an application that automatically downloads files from a public FTP site or search the Internet site for references to graphics and download only the graphics.
Download Internet File
Note: In order to provide some visual feedback to the user you could add a line like FlashWindow(Application.Handle,True) in the body of the repeat/until block. The FlashWindow API call flashes the caption of your applications name in the task bar.
function GetInetFile (const fileURL, FileName: String): boolean; const BufferSize = 1024; var hSession, hURL: HInternet; Buffer: array[1..BufferSize] of Byte; BufferLen: DWORD; f: File; sAppName: string; begin result := false; sAppName := ExtractFileName(Application.ExeName) ; hSession := InternetOpen(PChar(sAppName), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0) ; try hURL := InternetOpenURL(hSession, PChar(fileURL), nil, 0, 0, 0) ; try AssignFile(f, FileName) ; Rewrite(f,1) ; repeat InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen) ; BlockWrite(f, Buffer, BufferLen) until BufferLen = 0; CloseFile(f) ; result := True; finally InternetCloseHandle(hURL) end finally InternetCloseHandle(hSession) end end;
To call the GetInetFile function you could use the next piece of code:
This code will get the RSS feed from this site, download it, and save it as 'About Delphi Programming RSS Feed.xml'.
var internetFile, localFileName: string; begin internetFile := 'http://0.tqn.com/6/g/delphi/b/index.xml'; localFileName := 'About Delphi Programming RSS Feed.xml'; if GetInetFile(internetFile, localFileName) then ShowMessage('Download successful.') else ShowMessage('Error in file download.') ;
Use the RSS Feed to create a simple Tree-Like RSS Reader.