Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

FTP File Transfer get() and put() Files Example

Written by attila, January 30th, 2008
// |================================================= =================================|
// |Program Name : FTPComponent |
// |Actual Vers. : 1.0 |
// |Author : Steve Begelman (Steve_Begelman@Yahoo.com) for TG |
// | |
// |Company : AXA >> La Défense |
// |Create Date : January 2007 |
// | |
// |%%SCCS%% Rev. : FTPComponent.cs |
// |SCCS Keywords : utility FTP COM FTPComponent |
// | |
// |Dependencies : This a "dll" Library Executable Class |
// | |
// |Stored Procs. : none : Uses File-System Ressources Exclusively |
// | : |
// |----------------------------------------------------------------------------------|
// |Revision Hist.: dd/mm/yy | Modification | By |
// |----------------------------------------------------------------------------------|
// | 1.0 : 16/12/08 | Open/Read/Close FTP in DLL | SB |
// | 1.1 : 18/12/08 | Example Driver Written | SB |
// |----------------------------------------------------------------------------------|
// | : |
// |Role : Manipulate FTP Files get & put |
// | : |
// | : Protocols: none |
// | : Stored Procedures: none |
// | : |
// |Compiler : Microsoft .NET 2.XX |
// |Compiler Opts.: /safe |
// | |
// |Comments : |
// | : "App.conf" XML file Must be Present on Startup in ../bin/debug |
// | : |
// +================================================= =================================+
//

using System;
using System.Diagnostics;
using System.Collections;
using System.Data;
using System.IO;
using System.Text;
using System.Net;

using System.Collections.Specialized;
using Microsoft.Win32;

namespace Axaim.Common.Foundation.Network.Utilities
{
public enum FTPReturnStatus
{
_None_,
Success,
FileNotFound,
BadURIFormat,
UploadError
}

public sealed class FTP
{
public string Uri
{
get { return _uri; }
}

public int WrittenBytes
{
get { return _writtenBytes; }
}

private string _uri = null;
private long _fileSize = 0;
private int _writtenBytes = 0;

public FTPReturnStatus FTPGetFile( string fileName, string completeQualifiedOutputFile, bool debug )
{
FileInfo fileInf = new FileInfo( fileName );
_fileSize = fileInf.Length;
WebClient client = new WebClient();
_uri = fileName;

if(debug)
{
System.Console.WriteLine("Fetching File {0}...\n", _uri);
}
string tempFile = completeQualifiedOutputFile;
try
{
client.DownloadFile(_uri, tempFile);
if(debug)
{
System.Console.WriteLine("Downloaded File: {0} to : {1} \n", _uri, tempFile);
}
}
catch( System.Net.WebException snwe )
{
if( debug )
{
System.Console.WriteLine("File Transfer Failed on Inexisteant File !" + snwe.ToString() + " " + snwe.Source);
}
return FTPReturnStatus.FileNotFound;
}
return FTPReturnStatus.Success;
}

public FTPReturnStatus FTPPutFile(string inputFileName, string RemoteDirectory, string ftpServerIP, string ftpUserID, string ftpPassword, bool debug )
{
FileInfo fileInf = new FileInfo(inputFileName);
_uri = "ftp://" + ftpServerIP + "/" + RemoteDirectory.Trim() + "/" + fileInf.Name.Trim();
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create
(new Uri("ftp://" + ftpServerIP + "/" + RemoteDirectory.Trim() + "/" + fileInf.Name.Trim()));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Proxy = null; // May Need to Pass Proxy Name Here ?
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;

int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
_writtenBytes = 0;

FileStream fs = fileInf.OpenRead();

try
{
if( debug )
{
System.Console.WriteLine("Upload Local File: {0} To Remote : {1} \n", inputFileName, _uri);
}

Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while( contentLen != 0 )
{
strm.Write( buff, 0, contentLen );
contentLen = fs.Read( buff, 0, buffLength );
_writtenBytes += contentLen;
}
strm.Close();
fs.Close();
}

catch(System.IO.FileNotFoundException sfnne)
{
if(debug)
{
System.Console.WriteLine("File Not Found Exception");
System.Console.WriteLine(sfnne.Message + " " + sfnne.Source + " " + sfnne.StackTrace);
}
return FTPReturnStatus.FileNotFound; // _fileError;
}

catch(System.UriFormatException uri_ex)
{
if(debug)
{
System.Console.WriteLine("URI Format Exception");
System.Console.WriteLine(uri_ex.Message + " " + uri_ex.Source + " " + uri_ex.StackTrace);
}
return FTPReturnStatus.BadURIFormat;
}

catch(Exception ex)
{
if(debug)
{
System.Console.WriteLine(ex.Message, "FTP Upload Error");
}
return FTPReturnStatus.UploadError;
}

if(debug)
{
System.Console.WriteLine(inputFileName + " " + "FTP Upload OK" + " " + _writtenBytes.ToString() + " " + "Bytes Written");
}
return FTPReturnStatus.Success;
}

} // Class

} // Namespace

Last edited by attila : January 30th, 2008 at 08:11 AM. Reason: Formating
0 Comments Posted ( Post your comment )

Stats:
Views: 905
Comments: 0