How to FTP file using VB.net

I need to FTP a file i created using VB.net.I am writing a console application.
zameer21Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fredmastroCommented:
Here's a class you could download free and use it.

http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=DD5E4A38-7F5B-45E0-9C36-64B987947C20

Download the Class.. then at the bottom you'll find the example code how to use it... I've pasted it here.  Just the example part.  You need to just add the clsFTP.vb file to your project, or use the FTP.dll in your Bin folder.


    '
    ' Copy and paste the code below into a VB WebForm or WinForm
    '  application and then do the following:
    '
    '       1).  From within the ASP.NET or WinForm app set a
    '            reference to the FTP.dll and BitOperators.dll
    '            files.
    '       2).  At the top of the application code file 
    '            (E.g WebForm1.aspx.vb or Form1.vb) type in
    '               Imports FTP
    '       3).  Compile the application and run.
    '       4).  Have fun.
 
    'Protected Sub TestFTP()
    '    Dim ff As clsFTP
 
    '    Try
    '        '-------------------------------------------
    '        ' OPTION 1
    '        ' --------
    '        '
    '        ' Create an instance of the FTP Class.
    '        'ff = New clsFTP()
 
    '        ' Setup the appropriate properties.
    '        'ff.RemoteHost = "microsoft"
    '        'ff.RemoteUser = "ftpuser"
    '        'ff.RemotePassword = "password"
    '        '-------------------------------------------
 
    '        '-------------------------------------------
    '        ' OPTION 2
    '        ' --------
    '        '  Pass the values into the constructor 
    '        '  instead.  These can be overridden by simply
    '        '  setting the appropriate properties on the
    '        '  instance of the clsFTP Class.
    '        ff = New clsFTP("microsoft", _
    '                        ".", _
    '                        "ftpuser", _
    '                        "password", _
    '                        21)
 
    '        ' Attempt to log into the FTP Server.
    '        If (ff.Login()) Then
    '            '
    '            ' Move the to Area1\Section1\Subby1\ directory.
    '            ff.ChangeDirectory("Area1")
    '            ff.ChangeDirectory("Section1")
 
    '            'ff.CreateDirectory("Subby1")
    '            ff.ChangeDirectory("Subby1")
    '            ff.SetBinaryMode(True)
 
    '            ' Upload a file.
    '            'ff.UploadFile("d:\general\secureapps.pdf")
 
    '            ' Download a file.
    '            'ff.DownloadFile("secureapps.pdf", "d:\general\secureapps.pdf")
 
    '            ' Remove a file from the FTP Site.
    '            If (ff.DeleteFile("secureapps.pdf")) Then
    '                Response.Write("File has been removed from FTP Site")
    '                'MessageBox.Show("File has been removed from FTP Site")
    '            Else
    '                Response.Write("Unable to remove file from FTP Site.  Message from server: " & ff.MessageString & "<br>")
    '                'MessageBox.Show("Unable to remove file from FTP Site")
    '            End If
 
    '            ' Rename a file on the FTP Site.
    '            'If (ff.RenameFile("secureapps.pdf", "newapp.pdf")) Then
    '            '    Response.Write("File has been renamed")
    '            '    MessageBox.Show("File has been renamed")
    '            'End If
 
    '            'ff.ChangeDirectory("..")
    '            'If (ff.RemoveDirectory("Subby1")) Then
    '            '    Response.Write("Directory has been removed<br>")
    '            '    ' MessageBox.Show("Directory has been removed")
    '            'Else
    '            '    Response.Write("Unable to remove the directory.  Message from server: " & ff.MessageString & "<br>")
    '            '    ' MessageBox.Show("Unable to remove the directory.")
    '            'End If
    '        End If
 
    '    Catch ex As System.Exception
    '        ' ASP.NET
    '        Response.Write(ex.Message & "<BR>")
    '        Response.Write("Message from FTP Server was: " & ff.MessageString)
 
    '        ' WinForms
    '        'Messagebox.Show(ex.Message)
    '        'MessageBox.show("Message from FTP Server was: " & ff.MessageString)
    '    Finally
    '        '
    '        ' Always close down the connection to ensure that
    '        '  there are no "stray" Fido's Fetching data.  In
    '        '  other words, no stray/limbo/not-in-use FTP
    '        '  connections.
    '        ff.CloseConnection()
    '    End Try
    'End Sub

Open in new window

0
Göran AnderssonCommented:
There is no need to download anything, it's already built into the framework.

You can use the UploadFile method in the WebClient class to send a file to an FTP server:

http://msdn2.microsoft.com/en-us/library/36s52zhs(VS.80).aspx

However, you don't even have to save the data to a file to upload it, you can use the UploadData method to send a byte array as a file to the FTP.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zameer21Author Commented:
Hi Greenhost,
i looked at the code in the link you send me,there is no place to put username and password.I need to ftp to a remote host with username and password.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

fredmastroCommented:
Could just use the class I linked you too.

    '        ' Create an instance of the FTP Class.
    '        'ff = New clsFTP()
 
    '        ' Setup the appropriate properties.
    '        'ff.RemoteHost = "microsoft"
    '        'ff.RemoteUser = "ftpuser"
    '        'ff.RemotePassword = "password"

0
zameer21Author Commented:
Hi FredMastro,
I am presenlty thinking of using ur class,i need to test it.I will be only able to do it tomm or day after, as i dont have all the ftp info yet.
0
Göran AnderssonCommented:
To set the user name and password for a WebClient object, you use the Credentials property:

myWebClient.Credentials = New NetworkCredential (username, password);
0
zameer21Author Commented:
GreenGhost,
where do u specify the remote host in the code you gave me.
0
zameer21Author Commented:
i GOT IT GREENHOST,
Is there way to determine if the file was written succesfully,through code.
UploadMathod returns Byte().What is this byte() array for.
0
Göran AnderssonCommented:
The method returns the response from the server.

I'm not certain what the FTP server returns as response, but I think that it's a message like "250 File action completed". You can use the Encoding.ASCII.GetString to decode the bytes into a string.
0
zameer21Author Commented:
GreenGhost:
I can write the file to the FTP server,but byte code returned when i use encoding.ascii.getstring is blank.
I would prefer to have sucees to failure so that i can write to the log.
0
Göran AnderssonCommented:
If there is something wrong, you will get an exception.

The method that I suggested for getting the resonse is also used in the example in the documentation for the UploadFile method:

http://msdn2.microsoft.com/en-us/library/aa329789.aspx
0
zameer21Author Commented:
I tried all that i was not particluarly happy with what it returns,
But i found another solution through code that uses FTPWebclient it is better and returns a better code,if you file was transmitted.I will divide points between GreenGhost and FredMastro,I dont like to use other dll,if i could code it myself.
0
Göran AnderssonCommented:
> code that uses FTPWebclient

Do you mean FtpWebRequest?
0
zameer21Author Commented:
yes thats correct FTPWEbRequest
0
Göran AnderssonCommented:
Yes, that would work too. The WebClient class is easier to use, but if you want a bit more control, the FtpWebRequest class is fine. Though it should still return the same response from the server for the same operation.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.