ARTICLE

Copy Files from System to FTP in .NET

Posted by Ankur Gupta Articles | Visual Basic 2010 July 17, 2010
In this article you can Upload files from your system To any FTP.
 
Reader Level:

You can use the following code to Upload files from your system to any FTP.

First add an OpenFIleDialog control on your Form.

Add a Browse button on your form.


Form1.JPG


Add the following code on Button Click Event.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim fdlg As New OpenFileDialog()

        fdlg.Title = "Select file"

        fdlg.InitialDirectory = "C:\"

        fdlg.FilterIndex = 1

        fdlg.Multiselect = True

        fdlg.RestoreDirectory = True

        Dim arr() As String

        Dim fileNameArray() As String

        If fdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then

            arr = fdlg.FileNames()

            fileNameArray = fdlg.SafeFileNames()

            Application.DoEvents()

            Dim tempFileName As String

            Try

                For i As Integer = 0 To arr.GetUpperBound(0) - 1

                    tempFileName = arr(i)

                    Dim clsRequest As System.Net.FtpWebRequest = _

                        DirectCast(System.Net.WebRequest.Create("Your Ftp Address " & fileNameArray(i)), System.Net.FtpWebRequest)

                    clsRequest.Credentials = New System.Net.NetworkCredential("FTP User Name", "FTP password")

                    clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

 

                    ' read in file...

                    Dim bFile() As Byte = System.IO.File.ReadAllBytes(tempFileName)

 

                    ' upload file...

                    Dim clsStream As System.IO.Stream = _

                        clsRequest.GetRequestStream()

                    clsStream.Write(bFile, 0, bFile.Length)

                    clsStream.Close()

                    clsStream.Dispose()

                Next

            Catch ex As Exception

                ' Some thing goes wrong

            End Try

        End If

    End Sub

After that run you application and click on brows button

Form22.JPG

Select all files which you want to upload on ftp and click Open button.
It will upload all files on FTP.

share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Diagram
Become a Sponsor