Framework - FTP Objects

FTP

FTP is an abbreviation for File Transfer Protocol. I suppose it came out of the Unix community as a tool to copy files from one location to another. These days there are many ways to do that, but sometimes you just have to use the FTP tool to transfer files.

Today I use a tool like FileZilla to transfer files to other computers. In some cases it uses FTP as a transport protocol.

But if I wanted, I could add code to my program to perform the same thing. In the past I have done this several times.

Samples

Copy File to FTP site

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Public Shared Function FS_2_FTP( _
ByVal FTPURL As String, _
ByVal UserName As String, _
ByVal Password As String, _
ByVal SourceDir As String, _
ByVal FileName As String)

Dim myFtpWebRequest As System.Net.FtpWebRequest
Dim myFtpWebResponse As System.Net.FtpWebResponse
Dim myStreamWriter As System.IO.StreamWriter

myFtpWebRequest = System.Net.WebRequest.Create(FTPURL + "/" + FileName)

If UserName <> "" Then
myFtpWebRequest.Credentials = New System.Net.NetworkCredential(UserName, Password)
End If

myFtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
myFtpWebRequest.UseBinary = True
myFtpWebRequest.KeepAlive = False

Try
myStreamWriter = New System.IO.StreamWriter(myFtpWebRequest.GetRequestStream())
Catch ex As Exception
'If ex.Message.Contains("500") Then
' ' the message is (500) syntax error, command unrecognized.
' ' Early tests indicate this only happens the first time. if
' ' that's true, lets see if executing this a second time will
' ' help.
' Try
' myStreamWriter = New System.IO.StreamWriter(myFtpWebRequest.GetRequestStream())
' Catch ex2 As Exception
' Return "FS_2_FTP: Error with GetRequestStream - " & ex2.Message
' End Try
'End If

Return "FS_2_FTP: Error with GetRequestSTream - " & ex.Message
End Try

myStreamWriter.Write(New System.IO.StreamReader(SourceDir + "\" + FileName).ReadToEnd)

myStreamWriter.Close()
myFtpWebResponse = myFtpWebRequest.GetResponse()

Dim results As String = myFtpWebResponse.StatusDescription

myFtpWebResponse.Close()
Return results

End Function

References