Question FTP upload problem

qwe2

New member
Joined
Jan 10, 2011
Messages
1
Programming Experience
Beginner
Hi!
I have some problems with the following code. I'm kinda new in programming.
I would like to do that i connect to a FTP server on Form load event and when i'm connected i want to upload a file overwriting the previous one. The code works perfect if i connect to the server each time before i want to upload but it doesn't seem to work in this way.

VB.NET:
Public Class Form1

    Public request As System.Net.FtpWebRequest
    Public strz As System.IO.Stream

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load   

request = DirectCast(System.Net.WebRequest.Create("*", System.Net.FtpWebRequest)
        request.Credentials = New System.Net.NetworkCredential("user", "pw")
        request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
        request.UsePassive = True



   Private Sub timerUpload_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerUpload.Tick

        Dim file() As Byte = System.IO.File.ReadAllBytes("c:\filename.txt")

        strz = request.GetRequestStream()
        strz.Write(file, 0, file.Length)
        strz.Close()
        strz.Dispose()
   End Sub

End Class

Thanks for your help.
 
You have to use a new request each upload. Simpler using WebClient class or My.Computer.Network.UploadFile method.
This is how these classes are designed, there is no resident connected ftp client in the .Net library. For that you have to use a third party, or implement the protocol yourself over sockets.
 
Back
Top