|
-
Dec 17th, 2011, 05:25 PM
#1
Thread Starter
New Member
FTP Uploader
I am trying to create a console application that will upload an updated .html file to a website each time I run it, I have found previous codes that download the files but I want to upload instead.
So basically I am trying to create an FTP program (but only one that uploads) in a console application
Does anyone know can this can be done?
Thanks
-
Dec 17th, 2011, 08:03 PM
#2
Re: FTP Uploader
Try this. You'll have to add error checking yourself.
Code:
FTPUpload.exe /f "ftp://somehost.com/FileToUpload.pdf" /n "C:\FileToUpload.pdf" /u "username" /p "password"
vb.net Code:
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
-
Dec 18th, 2011, 09:16 AM
#3
Thread Starter
New Member
Re: FTP Uploader
Yes!! that works thanks very much, been searching around to find out how to do it for a while now.
Thanks
-
Dec 18th, 2011, 04:39 PM
#4
Thread Starter
New Member
Re: FTP Uploader
 Originally Posted by Chris001
Sub Main(ByVal args As String())
That code works on its own great, but now I am trying to implement it into another program but have a problem, I have changed the name of that sub to "write" but when I try to then make the program go to that sub from somewhere else by doing write() I have to pass it the thing for the args but I'm not sure where the value for args came from? So how can I run that sub from somewhere else?
Hope that makes sense
Thanks
Update:
Actually don't worry, I have figured it out
Last edited by nickbu1; Dec 18th, 2011 at 04:44 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|