upload a file using ftp

Henk

Member
Joined
Feb 12, 2006
Messages
7
Programming Experience
Beginner
Hi,

I want to upload a file using ftp from within a VB.NET program.

How can I do this?
 
With the FtpWebRequest and FtpWebResponse classes?
VB.NET:
[COLOR=black][FONT=Verdana][COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] Upload([COLOR=blue]ByVal[/COLOR] source [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR], [COLOR=blue]ByVal[/COLOR] target [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR], _
[COLOR=blue]ByVal[/COLOR] credential [COLOR=blue]As[/COLOR] NetworkCredential)
[COLOR=blue]  Dim[/COLOR] request [COLOR=blue]As[/COLOR] FtpWebRequest = _
        [COLOR=blue]DirectCast[/COLOR](WebRequest.Create(target), FtpWebRequest)
    request.Method = WebRequestMethods.Ftp.UploadFile
    request.Credentials = credential
    [COLOR=blue]Dim[/COLOR] reader [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] FileStream(source, FileMode.Open)
    [COLOR=blue]Dim[/COLOR] buffer(Convert.ToInt32(reader.Length - 1)) [COLOR=blue]As[/COLOR] [COLOR=blue]Byte[/COLOR]
    reader.Read(buffer, 0, buffer.Length)
    reader.Close()
    request.ContentLength = buffer.Length
    [COLOR=blue]Dim[/COLOR] stream [COLOR=blue]As[/COLOR] Stream = request.GetRequestStream
    stream.Write(buffer, 0, buffer.Length)
    stream.Close()
    [COLOR=blue]Dim[/COLOR] response [COLOR=blue]As[/COLOR] FtpWebResponse = [COLOR=blue]DirectCast[/COLOR](request.GetResponse, FtpWebResponse)
    MessageBox.Show(response.StatusDescription, [COLOR=maroon]"File Uploaded"[/COLOR])
    response.Close()
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR][/FONT][/COLOR]

and call with something like:
VB.NET:
[COLOR=black][FONT=Verdana][COLOR=blue]Dim[/COLOR] credential [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] NetworkCredential([COLOR=maroon]"username"[/COLOR], [COLOR=maroon]"password"[/COLOR])
Upload([COLOR=maroon]"test.zip"[/COLOR], [COLOR=maroon]"ftp://myftp.net/test.zip"[/COLOR], credential)[/FONT][/COLOR]
 
install...

Hi,

I just installed .Net framework 2 + the SDK but I still don't have
the ftpwebrequest class.

Do I need to remove the framework 1.1 first?
Do I need to install visual studio 2005? I have 2003...

Do I need to install something else???
 
VS 2003 doesn't do .NET 2.0 and uses the .NET 1.1 framework.
You could write the code without IDE and compile against the .NET 2.0 framework, or use VS2005.
The links in my previous post work for .NET 1.1 and with VS2003 though.
 
Back
Top