Urvashi
Urvashi

Reputation: 504

Connect to FTP server and download file from FTP to local machine

I need to know a way to connect to a FTP site and i am unable to find an example to do the program using C#. I need to write the code where i could connect, and download files from the FTP server without using third party component.

How can i do this ? Help.

Upvotes: 1

Views: 13292

Answers (2)

dan_l
dan_l

Reputation: 1692

There is FtpWebRequest class in .Net 4

http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx

There are examples at the end. Here is a sample taken from msdn:

public static bool DisplayFileFromServer(Uri serverUri)
{
    // The serverUri parameter should start with the ftp:// scheme.
    if (serverUri.Scheme != Uri.UriSchemeFtp)
    {
        return false;
    }
    // Get the object used to communicate with the server.
    WebClient request = new WebClient();

    // This example assumes the FTP site uses anonymous logon.
    request.Credentials = new NetworkCredential ("anonymous","[email protected]");
    try 
    {
        byte [] newFileData = request.DownloadData (serverUri.ToString());
        string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
        Console.WriteLine(fileString);
    }
    catch (WebException e)
    {
        Console.WriteLine(e.ToString());
    }
    return true;
}

Upvotes: 4

Dave Hogan
Dave Hogan

Reputation: 3221

This isn't specifically a question as such.

You need to use the socket classes within the .NET framework: MSDN - System.Net.Sockets

A good example I've previously used is: www.dreamincode.net - Create an ftp class library

Upvotes: 1

Related Questions