Sign In
IRhetoric - Karsten Januszewski
Avalon: Convergence in the Simulacrum
Translate This Page
Translate this page
Powered by
Microsoft® Translator
Options
Blog Home
Email Blog Author
Share this
RSS for posts
Atom
RSS for comments
Search
Advanced search options...
Search In:
Everything
Blogs
Forums
People
Groups
Places
Pages
Date range:
All Time
Last Year
Last 6 Months
Last 3 Months
Last Month
Last Week
Last Two Days
Tags
.NET Framework 2.0 (Whidbey)
3D
Action Script
Avalon
Expression Blend
Facebook
Flash
MIX07
Pages
Philosophy
sxsw
Twitter
Vista
WCF
weblive
Windows Communication Foundation
Windows Presentation Foundation (Avalon)
WPF
WPF TWITTER
WPF/e
Archive
Archives
August 2007
(1)
June 2007
(6)
May 2007
(7)
April 2007
(2)
March 2007
(7)
February 2007
(5)
January 2007
(8)
December 2006
(5)
November 2006
(6)
October 2006
(9)
September 2006
(2)
August 2006
(3)
July 2006
(3)
June 2006
(12)
May 2006
(8)
April 2006
(7)
March 2006
(4)
February 2006
(8)
January 2006
(9)
December 2005
(4)
November 2005
(9)
October 2005
(10)
September 2005
(4)
August 2005
(1)
July 2005
(5)
June 2005
(19)
May 2005
(9)
April 2005
(4)
March 2005
(1)
February 2005
(2)
January 2005
(3)
December 2004
(1)
November 2004
(4)
August 2004
(2)
July 2004
(3)
June 2004
(3)
May 2004
(3)
April 2004
(5)
March 2004
(3)
February 2004
(3)
FTP Sample Using Whidbey (.NET Framework 2.0 Beta 2)
MSDN Blogs
>
IRhetoric - Karsten Januszewski
>
FTP Sample Using Whidbey (.NET Framework 2.0 Beta 2)
FTP Sample Using Whidbey (.NET Framework 2.0 Beta 2)
karstenj
30 Nov 2004 2:08 PM
Comments
1
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;
}
}
}
1 Comments
.NET Framework 2.0 (Whidbey)
Blog - Comment List MSDN TechNet
Comments
Loading...