Hi everyone,
I am writing an application that downloads files from an FTP site in VB.Net, depending on the file name provided in the textbox on the screen.
The program currently writes the file to my desktop, however, the content of the file is not being downloaded, therefore the file size is 0 bytes.
Below is my code, can anybody advise on how I can edit my code so that my application not only creates the file on my desktop, but then when I open the file, the content is also there.
I feel at the moment that my problem lies with the way I have supplied the textbox details in the WebRequest decalartion, but I am not yet sure.
Any help would be appreciated.
Thanks in advance,
Dan
Protected Sub Button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button5.Click
Dim ftp As FtpWebRequest = DirectCast(WebRequest.Create(ftp://XX.XX.X.XX/In/ + TextBox1.Text), FtpWebRequest)
If TextBox1.Text = "" Then
MsgBox("A file needs to be requested!") End If
MsgBox("BEGINNING...")
If TextBox1.Text <> "" Then
Try
ftp.Credentials = New System.Net.NetworkCredential(User, password) ftp.KeepAlive = False
ftp.Timeout = 20000
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile ftp.UseBinary = True
' read in file...
Dim filestream As New FileStream("C:\Users\" + TextBox1.Text, FileMode.Append) Dim buffer(filestream.Length) As Byte
MsgBox("CONTINUING...")
filestream.Write(buffer, 0, filestream.Length)
MsgBox("CONTINUING MORE...")
' download file...
Dim GetStream As Stream = ftp.GetRequestStream() GetStream.Write(buffer, 0, filestream.Length)
GetStream.Close()
filestream.Close()
Dim ftpResponse As FtpWebResponse = CType(ftp.GetResponse(), FtpWebResponse)
MsgBox("FINISHED...")
Catch ex As Exception
End Try
End If
End Sub
I am writing an application that downloads files from an FTP site in VB.Net, depending on the file name provided in the textbox on the screen.
The program currently writes the file to my desktop, however, the content of the file is not being downloaded, therefore the file size is 0 bytes.
Below is my code, can anybody advise on how I can edit my code so that my application not only creates the file on my desktop, but then when I open the file, the content is also there.
I feel at the moment that my problem lies with the way I have supplied the textbox details in the WebRequest decalartion, but I am not yet sure.
Any help would be appreciated.
Thanks in advance,
Dan
Protected Sub Button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button5.Click
Dim ftp As FtpWebRequest = DirectCast(WebRequest.Create(ftp://XX.XX.X.XX/In/ + TextBox1.Text), FtpWebRequest)
If TextBox1.Text = "" Then
MsgBox("A file needs to be requested!") End If
MsgBox("BEGINNING...")
If TextBox1.Text <> "" Then
Try
ftp.Credentials = New System.Net.NetworkCredential(User, password) ftp.KeepAlive = False
ftp.Timeout = 20000
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile ftp.UseBinary = True
' read in file...
Dim filestream As New FileStream("C:\Users\" + TextBox1.Text, FileMode.Append) Dim buffer(filestream.Length) As Byte
MsgBox("CONTINUING...")
filestream.Write(buffer, 0, filestream.Length)
MsgBox("CONTINUING MORE...")
' download file...
Dim GetStream As Stream = ftp.GetRequestStream() GetStream.Write(buffer, 0, filestream.Length)
GetStream.Close()
filestream.Close()
Dim ftpResponse As FtpWebResponse = CType(ftp.GetResponse(), FtpWebResponse)
MsgBox("FINISHED...")
Catch ex As Exception
End Try
End If
End Sub