FTP Sample Using Whidbey (.NET Framework 2.0 Beta 2)

I recently needed to upload a bunch of images via FTP.  This gave me a perfect reason to write some code using the new FTP classes in Whidbey.  Very nice, I must say. Below is the source code for anyone needing to grab a directory and dump a bunch of files to an FTP site.   I write a success and failure log file to cope with an exceptions -- the way I write to the log is a bit clunky, but I was in a hurry!

 

  

  #region Using directives

 

using System;
using System.Text;
using System.IO;
using System.Net;
using System.Collections;
#endregion

 

namespace ImageFTPService
{

 

 class Program
{
private const string directory = @"";
private const string server = @"";
private const string username = @"";
private const string password = @"";
private const string log_path_success = @"C:\logs\success.log";
private const string log_path_fail = @"C:\logs\fail.log";
private static int errCount = 0;
static void Main(string[] args)
{
DirectoryInfo dir = new DirectoryInfo(directory);
FileInfo[] infos = dir.GetFiles("*.*");
bool IsSuccess = true;

 

   foreach (FileInfo fileInfo in infos)
{
if (null != fileInfo)
{
IsSuccess = UploadFile(fileInfo);
if (!IsSuccess)
{
errCount = errCount + 1;
if (errCount > 100)
break;
}
}
}
Console.WriteLine("Waiting...");
Console.Read();
}

 

  static bool UploadFile(FileInfo fileInfo)
{
bool IsSuccess = true;
NetworkCredential credentials = new NetworkCredential(username, password);
string serverAddress = "ftp://" + server + "/Images/";
FtpWebRequest request = null;
FtpWebResponse response = null;
FileStream fr = null;
try
{
request = (FtpWebRequest)FtpWebRequest.Create(new Uri(serverAddress + fileInfo.Name));
}

 

   catch (Exception ex)
{
using (StreamWriter sw3 = File.AppendText(log_path_fail))
{
sw3.WriteLine(fileInfo.Name);
sw3.WriteLine("-{0}", ex.Message);
}
Console.WriteLine("Fail: {0}", fileInfo.Name);
Console.WriteLine("-{0}", ex.Message);

 

    return false;

 

   }
try
{
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = credentials;
request.UseBinary = true;
//read the image into a byte array using a FileStream
byte[] fileContents = new byte[fileInfo.Length];
fr = fileInfo.OpenRead();
fr.Read(fileContents, 0, Convert.ToInt32(fileInfo.Length));
fr.Close();
//get the FTP upload stream
Stream requestStream = request.GetRequestStream();
//write the btye array to the stream
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
response = (FtpWebResponse)request.GetResponse();
using (StreamWriter sw0 = File.AppendText(log_path_success))
{
sw0.WriteLine(fileInfo.Name);
}
Console.WriteLine("Success: {0}", fileInfo.Name);
response.Close();
}
catch (Exception e)
{
using (StreamWriter sw2 = File.AppendText(log_path_fail))
{
sw2.WriteLine(fileInfo.Name);
sw2.WriteLine("-{0}", e.Message);
}
Console.WriteLine("Fail: {0}", fileInfo.Name);
Console.WriteLine("-{0}", e.Message);
IsSuccess = false;
}
System.Threading.Thread.Sleep(5000);
return IsSuccess;
}
}
}