Click here to Skip to main content
15,916,188 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I need download file from ftp site in my vb .NET Project.
I use Net.FtpWebRequest namespace.

When downloding following filename show error :

%EMON%EMON%%ACCEPTED%39fea100808rk1oo000dr9qo.XML

When I delete 39 in filename everything is OK. Maybe 39 as carriage return ...

Please help me ... What can I do for correct this problem.

Thanks
Posted

1 solution

Public Sub DownloadEachFile()
If ftpPortNo > 0 Then
ftpAdres = "ftp://" & ftpAdres & ":" & ftpPortNo & "/"
Else
ftpAdres = "ftp://" & ftpAdres & "/"
End If

Dim fwr As Net.FtpWebRequest = Net.FtpWebRequest.Create(ftpAdres)
fwr.Credentials = New NetworkCredential(ftpKullanıcıAdı.Trim, ftpŞifre.Trim)
fwr.KeepAlive = True
fwr.Method = WebRequestMethods.Ftp.ListDirectory
fwr.Proxy = Nothing

Try

Dim sr As New IO.StreamReader(fwr.GetResponse().GetResponseStream())
Dim lst = sr.ReadToEnd().Split(vbNewLine)
For Each file As String In lst

file = file.Trim() 'remove any whitespace
Dim LFile As String = YerelDosyaAdı(file.Trim)
If file = ".." OrElse file = "." Then Continue For
Dim fwr2 As Net.FtpWebRequest = Net.FtpWebRequest.Create(ftpAdres & file.Trim)
fwr2.Credentials = fwr.Credentials
fwr2.KeepAlive = True
fwr2.Method = WebRequestMethods.Ftp.DownloadFile
fwr2.Proxy = Nothing

Try
Using FtpResponse As FtpWebResponse = CType(fwr2.GetResponse, FtpWebResponse)
Using ResponseStream As IO.Stream = FtpResponse.GetResponseStream
Using fs As New IO.FileStream("C:\COSTOMERDB\" & LFile.Trim, 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
ResponseStream.Close()
fs.Flush()
fs.Close()

End Using
ResponseStream.Close()

End Using
End Using

Catch

End Try
Next

sr.Close()
Catch ex As Exception

End Try
End Sub
 
Share this answer
 

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