Articles > Development
Printer Friendly Version
Views: 9180

vb.net ftp class (uses ftp.exe built into windows)

Last Updated: 4/8/10


 Public Class FTP
            Friend Host As String = ""
            Friend username As String = ""
            Friend Password As String = ""

            Sub DownloadFile(ByVal source As String, ByVal dest As String)
                ' source = /parent/sub/filename.txt
                ' dest = "c:\parent\sub\filename.txt"
                Dim oWriter As IO.StreamWriter
                Dim strTemp As String = Environment.GetEnvironmentVariable("temp")
                Dim strFtpFile As String = strTemp & "\ftp.txt"
                Dim WINDIR As String = Environment.GetEnvironmentVariable("windir")
                Dim exec As String = WINDIR & "\system32\ftp.exe"
                Dim strArgs As String = "-s:" & strFtpFile & " " & Host

                If IO.File.Exists(strFtpFile) Then
                    Try
                        IO.File.Delete(strFtpFile)
                    Catch ex As Exception
                        log("Failed to delete ftp file!")
                    End Try
                End If

                Try
                    oWriter = New IO.StreamWriter(strFtpFile, True)
                    oWriter.WriteLine(username)
                    oWriter.WriteLine(Password)
                    oWriter.WriteLine("GET " & source & " " & dest)
                    oWriter.WriteLine("bye")
                    oWriter.WriteLine("quit")
                    oWriter.Close()
                    oWriter.Dispose()
                    oWriter = Nothing
                Catch ex As Exception
                    log("error writing ftp file. error: " & ex.Message)
                End Try



                Try
                    Dim oProc As New Diagnostics.Process
                    Dim pInfo As New Diagnostics.ProcessStartInfo

                    pInfo.FileName = exec
                    pInfo.Arguments = strArgs
                    pInfo.WindowStyle = ProcessWindowStyle.Hidden

                    oProc.StartInfo = pInfo
                    oProc.Start()
                    WaitForFTP()
                Catch ex As Exception
                    log("error launching ftp process (" & exec & "). error: " & ex.Message)
                End Try


                Try
                    IO.File.Delete(strFtpFile)
                Catch ex As Exception

                End Try

            End Sub


            Sub UploadFile(ByVal strFile As String, ByVal strPath As String)
                Dim oWriter As IO.StreamWriter
                Dim strTemp As String = Environment.GetEnvironmentVariable("temp")
                Dim strFtpFile As String = strTemp & "\ftp.txt"
                Dim WINDIR As String = Environment.GetEnvironmentVariable("windir")
                Dim exec As String = WINDIR & "\system32\ftp.exe"
                Dim strArgs As String = "-s:" & strFtpFile & " " & Host

                If IO.File.Exists(strFtpFile) Then
                    Try
                        IO.File.Delete(strFtpFile)
                    Catch ex As Exception
                        log("Failed to delete ftp file!")
                    End Try
                End If

                Try
                    oWriter = New IO.StreamWriter(strFtpFile, True)
                    oWriter.WriteLine(username)
                    oWriter.WriteLine(Password)
                    oWriter.WriteLine("CD " & strPath)
                    oWriter.WriteLine("PUT " & strFile)
                    oWriter.WriteLine("bye")
                    oWriter.WriteLine("quit")
                    oWriter.Close()
                    oWriter.Dispose()
                    oWriter = Nothing
                Catch ex As Exception
                    log("error writing ftp file. error: " & ex.Message)
                End Try



                Try
                    Dim oProc As New Diagnostics.Process
                    Dim pInfo As New Diagnostics.ProcessStartInfo

                    pInfo.FileName = exec
                    pInfo.Arguments = strArgs
                    pInfo.WindowStyle = ProcessWindowStyle.Hidden

                    oProc.StartInfo = pInfo
                    oProc.Start()
                    WaitForFTP()
                Catch ex As Exception
                    log("error launching ftp process (" & exec & "). error: " & ex.Message)
                End Try

               

                Try
                    IO.File.Delete(strFtpFile)
                Catch ex As Exception

                End Try
            End Sub

            Sub WaitForFTP()
                Dim a As Integer = 0

                Threading.Thread.Sleep(1000)
                While Diagnostics.Process.GetProcessesByName("ftp").Length > 0
                    a += 1
                    Threading.Thread.Sleep(1000)

                    If a > 300 Then
                        Exit While
                    End If
                End While
            End Sub
        End Class












Keywords: vb.net ftp client sampel example ftp put get transfer file code