Imports System.Net
Imports System.Threading
Module FTPUpload
Dim FTP As New WebClient
Dim ManualEvent As ManualResetEvent
Dim UploadTime As New Stopwatch
Sub Main(ByVal args As String())
Dim RemotePath As String = String.Empty
Dim LocalFile As String = String.Empty
Dim Username As String = String.Empty
Dim Password As String = String.Empty
Dim i As Integer
For i = 0 To args.Length - 1
Select Case args(i)
Case "/f"
i += 1
RemotePath = args(i)
Case "/n"
i += 1
LocalFile = args(i)
Case "/u"
i += 1
Username = args(i)
Case "/p"
i += 1
Password = args(i)
Case Else
Console.WriteLine("Invalid parameters!")
Console.ReadKey()
Return
End Select
Next
Console.WriteLine("")
Console.WriteLine("Uploading file to the server. Please wait...")
Console.WriteLine("")
AddHandler FTP.UploadProgressChanged, AddressOf UploadProgressChanged
AddHandler FTP.UploadFileCompleted, AddressOf UploadFileCompleted
FTP.Credentials = New NetworkCredential(Username, Password)
FTP.UploadFileAsync(New Uri(RemotePath), LocalFile)
UploadTime.Start()
ManualEvent = New ManualResetEvent(False)
ManualEvent.WaitOne()
End Sub
Private Sub UploadProgressChanged(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs)
Dim FixedProgress As Integer = If(e.ProgressPercentage <> 100, e.ProgressPercentage * 2, e.ProgressPercentage)
Console.Write("{0}Upload Progress: {1} bytes / {2} bytes - {3}%", vbCr, e.BytesSent, e.TotalBytesToSend, FixedProgress)
End Sub
Private Sub UploadFileCompleted(ByVal sender As Object, ByVal e As UploadFileCompletedEventArgs)
Console.WriteLine("")
If e.Error IsNot Nothing Then
Console.WriteLine(e.Error.Message.ToString)
Else
Console.WriteLine("Upload successfully finished in {0} seconds!", UploadTime.Elapsed.TotalSeconds)
End If
Console.WriteLine("")
Console.WriteLine("Press any key to close this window...")
Console.ReadKey()
UploadTime.Stop()
ManualEvent.Set()
End Sub
End Module