Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am currently working on a project where my client needs to import data from an ftp site at the moment they are doing it manually and i need to automate it.
I would like to know how would I be able to allow the user when they click on the button , the program will fetch all the files from the FTP Server and add them into a directory(../Test) on my server

Thanks
Posted

1 solution

Imports System.Collections.Generic
Imports Utilities.FTP

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'EXAMPLE OF HOW TO USE FtpWebRequest

'Values to use
Const localFile As String = "C:\look.com"

Const remoteFile As String = "/public_html/fm.com"
Const host As String = "ftp://users.adelphia.net/"
Const username As String = "gcumbia"
Const password As String = "MyPassword"

'Create a request
Dim URI As String = host & remoteFile
Dim ftp As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(URI), System.Net.FtpWebRequest)
'Set the credentials
ftp.Credentials = New System.Net.NetworkCredential(username, password)
'Turn off KeepAlive (will close connection on completion)
ftp.KeepAlive = False
'we want a binary
ftp.UseBinary = True
'Define the action required (in this case, download a file)
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile

'If we were using a method that uploads data e.g. UploadFile
'we would open the ftp.GetRequestStream here an send the data

'Get the response to the Ftp request and the associated stream
Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)
Using responseStream As IO.Stream = response.GetResponseStream
'loop to read & write to file
Using fs As New IO.FileStream(localFile, IO.FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
Do
read = responseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0 'see Note(1)
responseStream.Close()
fs.Flush()
fs.Close()
End Using
responseStream.Close()
End Using
response.Close()
End Using

End Sub

'An example of FtpClient usage
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

'Create a link to an FtpServer
Dim ftp As New FTPclient("users.adelphia.net", "gcumbia", "MyPassword")

'download a file (permitting overwrite)
'ftp.Download("/pub/ftpfile.bin", "C:\test\ftp\ftpfile.bin", True)

'upload a file
ftp.Upload("c:\look.com", "/public_html/look.com")

'does a file exist?
'If ftp.FtpFileExists("/pub/upload.exe") Then
' 'rename a file
' ftp.FtpRename("/pub/upload.exe", "/pub/newname.exe")
' 'delete a file
' ftp.FtpDelete("/pub/newname.exe")
'End If

'get a simple listing of the root directory /
Dim result As List(Of String) = ftp.ListDirectory("/")
'display in console
Console.WriteLine("Directory listing of /")
For Each line As String In result
Console.WriteLine(line)
Next

'Get the detailed directory listing of /pub/
Dim dirList As FTPdirectory = ftp.ListDirectoryDetail("/public_html/")

'filter out only the files in the list
Dim filesOnly As FTPdirectory = dirList.GetFiles()

'download these files
'For Each file As FTPfileInfo In filesOnly
' ftp.Download(file, "C:\test\ftp\" & file.Filename, True)
'Next


End Sub
End Class
 
Share this answer
 
Comments

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900