string host = "ftp.myFtpSite.com";
string username = "anonymous";
string password = "your@email.com";
string file = "myLocalFile.txt";
if (!host.StartsWith("ftp://"))
{
host = "ftp://" + host;
}
Uri uri = new Uri(host);
System.IO.FileInfo info = new System.IO.FileInfo(file);
string destFileName = host + "/" + info.Name;
try
{
//yes, even though this is FTP, we can use the WebClient
//it will see the ftp:// and use the appropriate protocol
//internally
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(username, password);
byte[] response = client.UploadFile(destFileName, file);
if (response.Length > 0)
{
Console.WriteLine("Response: {0}",
Encoding.ASCII.GetString(response));
}
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
}