Jun 7, 2011

Download a file from FTP server C#

Download a file from FTP server C#

In .Net it is always easy to write a piece of code as most of the functionalities are in built.
System.Net namespace inclusion does the work for you and it is very simple. 

Just go through the piece of code/function written below.
static void downloadFile()
        {
            //Create a WebClient.
            WebClient request = new WebClient();

            //Setup the credentials to the ftp server
            request.Credentials =
                new NetworkCredential("username",
                                        "password");

            //Download the data into a Byte array

            //@ is used to take the relative path
            byte[] fileData =
                request.DownloadData(@"Ftp path" + @"/" +
                                     @"Directorypath");

            //Create a FileStream that we'll write the byte array to.
                FileStream file =
                File.Create(@"C:\sand\Weekly Plan Excel-10-sep.xls");

            //Write the full byte array to the file.
            file.Write(fileData, 0, fileData.Length);

            //Close the file so other processes can access it.
            file.Close();
        }
        
it is as simple as that ...

Hope it helped u ....

Cheers!!!!!!!!!
Sandeep

No comments: