通过 VB.Net 从 FTP 服务器复制到本地服务器






2.56/5 (10投票s)
2007年5月10日

86749
这段代码可以帮助你了解如何从 FTP 服务器复制/下载文件到本地服务器。
引言
阅读代码,你就会知道如何使用 VB.Net 代码从 FTP 服务器将文件复制或下载到你的本地服务器/机器。
背景
手动将代码从 FTP 服务器复制到本地服务器总是很有趣,但如果通过代码实现,这项工作会变得繁琐……
使用代码
这是我最优秀的作品之一,可以将多个文件从任何 FTP 服务器复制到你想要的文件夹或服务器。请阅读代码,体验它的美妙之处……
// // Any source code blocks look like this // Dim oScript, oFileSys, objFSO, fileSearchObj, fileNameList, fileName, fileNameArray Dim tempFileName As String Try '' set the FTP path and username/password fields Dim ftp_address As String = "ftp.XYZ.com" '---> ftp site address Dim ftp_username As String = "username" '---> user name Dim ftp_password As String = "passwd" '---> password fileSearchObj = CreateObject("Scripting.FileSystemObject") ' Get all the files down loded to C:\ftpDownload folder fileNameArray = fileSearchObj.GetFolder("C:\ftpDownload\") fileNameList = fileNameArray.files For Each fileName In fileNameList tempFileName = fileName.name '1. Crate the temperory .ftp file (ftpCommand.ftp) which contain the FTP code '2. Write the code for copying the file from FTP to Local folder '3. Write the code to delete the file from FTP after copy '4. Execute the file through command prompt Dim objTextFile Dim strFTPFileName As String = "" '----------------------------------------------------------- ' Step 1 '----------------------------------------------------------- ' this file will be saved on the root folder (bydefault) strFTPFileName = "C:\ftpCommand.ftp" ' create the temporary on root folder objTextFile = objFSO.CreateTextFile(strFTPFileName, True) '----------------------------------------------------------- ' Step 2 '----------------------------------------------------------- ' write the ftp command line code to fetch files in the file test.ftp objTextFile.WriteLine("open " & ftp_address) '-------> open FTP site/location objTextFile.WriteLine(ftp_username) '--------------> ftp user name objTextFile.WriteLine(ftp_password) '--------------> ftp user password objTextFile.WriteLine("cd tempFolder") '-----------> cd tempFolder ' tempFolder is the source folder on FTP site from where application will down load files ' Example- ftp:\\ftp.XYZ.com\tempFolder objTextFile.WriteLine("lcd C:\ftpDownload") '--------> This is the working directory '----------------------------------------------------------- ' Step 3 '----------------------------------------------------------- objTextFile.WriteLine("delete " & tempFileName) '----> delete 'filename' // to delete the same copied files from the FTP server objTextFile.WriteLine("y") '--------------> y //propmt message 'yes' to commit the copy operation objTextFile.WriteLine("bye") '--------------> bye //ftp command to come out of the ftp command promt ' close the stream for creating the new ftp file objTextFile.Close() objTextFile = Nothing '----------------------------------------------------------- ' Step 4 '----------------------------------------------------------- ' create the FTP command promt syntax to execute the files Dim strCMD As String strCMD = "ftp.exe -i -s: C:\ftpCommand.ftp" ' create the temperory file to store the output generated after executing the FTP command Dim strTempFile As String strTempFile = "C:\ftpOutput.txt" ' call the command promt control to execute the delete operation Call oScript.Run("cmd.exe /c " & strCMD & " > " & strTempFile, 0, True) Next Catch ex As Exception ' Some thing goes wrong End TryYou can use the desire desination folder instead of harc-coded C:\ drive