-
FTP Utility class
Simplifies FTP access (slightly) by wrapping the system.net.ftpWebRequest class methods:-
Code:
using System;
using System.Net;
namespace FileTransfers
{
/// <summary>
/// Class to facilitate sending and receiving files by FTP
/// </summary>
public static class FTPFileTransfer
{
/// <summary>
/// Uploads the named file to the FTP target site
/// </summary>
/// <param name="filename">The file to send to the FTP site</param>
/// <param name="address">The address of the machine and folder to send the file to</param>
/// <param name="ftpUsername" >The user name to connect to the FTP target as</param>
/// <param name="ftpPassword" >The password to connect to the FTP target with</param>
/// <returns >
/// True if the file was succesfully uploaded, otherwise false
/// </returns>
public static bool FTPUploadFile(System.IO.FileInfo filename,
string address,
string ftpUsername,
string ftpPassword)
{
bool _uploadSucceeded = false;
byte[] buf = new byte[2048];
int iWork;
System.Uri _targetURI = new System.Uri("ftp://" + address + @"/" + filename.Name);
FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(_targetURI);
req.Credentials = new NetworkCredential(ftpUsername,
ftpPassword);
System.IO.Stream rs = null;
System.IO.FileStream fs = null;
req.UsePassive = false;
req.KeepAlive = false;
req.Method = WebRequestMethods.Ftp.UploadFile;
try
{
rs = req.GetRequestStream();
fs = System.IO.File.Open(filename.FullName, System.IO.FileMode.Open);
while ((iWork = fs.Read(buf, 0, buf.Length)) > 0)
rs.Write(buf, 0, iWork);
if (rs != null)
{
rs.Close();
rs.Dispose();
}
FtpWebResponse wrRet = ((FtpWebResponse)req.GetResponse());
if (wrRet.StatusCode == FtpStatusCode.CommandOK || wrRet.StatusCode == FtpStatusCode.ClosingData || wrRet.StatusCode == FtpStatusCode.ClosingControl || wrRet.StatusCode == FtpStatusCode.ConnectionClosed || wrRet.StatusCode == FtpStatusCode.FileActionOK)
{
_uploadSucceeded = true;
}
else
{
System.Diagnostics.Trace.TraceError(string.Concat(filename.FullName, " ftp status code: ", wrRet.StatusCode.ToString()));
_uploadSucceeded = false;
}
wrRet.Close();
}
catch (System.Exception ex)
{
System.Diagnostics.Trace.TraceError(ex.ToString());
_uploadSucceeded = false;
}
finally
{
if (fs != null)
{
fs.Close();
fs.Dispose();
}
if (rs != null)
{
rs.Close();
rs.Dispose();
}
}
// Return the FTP process status to the caller
return _uploadSucceeded;
}
/// <summary>
/// Downloads a named file from the FTP directory
/// </summary>
/// <param name="targetFile">The filename on the FTP site</param>
/// <param name="sourceFileName">The filename to save it as locally</param>
/// <param name="address">The address of the machine and folder to get the file from</param>
/// <param name="ftpUsername">The user name to connect to the FTP target as</param>
/// <param name="ftpPassword">The password to connect to the FTP target with</param>
/// <returns></returns>
public static bool FTPDownloadFile(System.IO.FileInfo targetFile,
string sourceFileName,
string address,
string ftpUsername,
string ftpPassword)
{
bool _downloadSucceeded = false;
byte[] buf = new byte[2048];
int iWork;
System.Uri _targetURI = new System.Uri("ftp://" + address + @"/" + targetFile.Name);
FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(_targetURI);
req.Credentials = new NetworkCredential(ftpUsername,
ftpPassword);
System.IO.Stream rs = null;
System.IO.FileStream fs = null;
req.UsePassive = false;
req.KeepAlive = false;
req.Method = WebRequestMethods.Ftp.DownloadFile;
try
{
FtpWebResponse wr = (FtpWebResponse)req.GetResponse();
rs = wr.GetResponseStream();
// Read the request stream into the target file
fs = System.IO.File.Open(sourceFileName, System.IO.FileMode.Create);
while (((iWork = rs.Read(buf, 0, buf.Length)) > 0))
fs.Write(buf, 0, iWork);
if (rs != null)
{
rs.Close();
rs.Dispose();
}
// Make sure the file is fully written
fs.Flush();
FtpWebResponse wrRet = ((FtpWebResponse)req.GetResponse());
if (wrRet.StatusCode == FtpStatusCode.CommandOK || wrRet.StatusCode == FtpStatusCode.ClosingData || wrRet.StatusCode == FtpStatusCode.ClosingControl || wrRet.StatusCode == FtpStatusCode.ConnectionClosed || wrRet.StatusCode == FtpStatusCode.FileActionOK)
{
_downloadSucceeded = true;
}
else
{
System.Diagnostics.Trace.TraceError(string.Concat(sourceFileName , " ftp status code: ", wrRet.StatusCode.ToString()));
_downloadSucceeded = false;
}
wrRet.Close();
}
catch (System.Exception ex)
{
System.Diagnostics.Trace.TraceError(ex.ToString());
_downloadSucceeded = false;
}
finally
{
if (fs != null)
{
fs.Close();
fs.Dispose();
}
if (rs != null)
{
rs.Close();
rs.Dispose();
}
}
// Return the FTP process status to the caller
return _downloadSucceeded;
}
}
-
Re: FTP Utility class
and to check a file exists or delete it from the FTP location...
Code:
/// <summary>
/// Returns true if the named file exists on the FTP target
/// </summary>
/// <param name="sourceFilename">The filename (with no PATH) to check for existence</param>
/// <param name="address">The FTP server and path</param>
/// <param name="ftpUsername">The user name to connect to the FTP target as</param>
/// <param name="ftpPassword">The password to connect to the FTP target with</param>
/// <returns>True if the named file exists</returns>
public static bool FTPFileExists(string sourceFilename,
string address,
string ftpUsername,
string ftpPassword)
{
bool _fileExists = false;
System.Uri _targetURI = new System.Uri("ftp://" + address + @"/" + sourceFilename);
FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(_targetURI);
req.Credentials = new NetworkCredential(ftpUsername,
ftpPassword);
req.UsePassive = false;
req.KeepAlive = false;
req.Method = WebRequestMethods.Ftp.GetFileSize;
try
{
FtpWebResponse wr = (FtpWebResponse)req.GetResponse();
if (wr.StatusCode == FtpStatusCode.CommandOK
|| wr.StatusCode == FtpStatusCode.ClosingData
|| wr.StatusCode == FtpStatusCode.ClosingControl
|| wr.StatusCode == FtpStatusCode.ConnectionClosed
|| wr.StatusCode == FtpStatusCode.FileActionOK
|| wr.StatusCode == FtpStatusCode.FileStatus)
{
_fileExists = true;
}
else
{
System.Diagnostics.Trace.TraceError(string.Concat(sourceFilename, " ftp status code: ", wr.StatusCode.ToString()));
_fileExists = false;
}
}
catch (Exception ex)
{
// If any error occured then the file effectively doesn't exist
System.Diagnostics.Trace.TraceError(ex.ToString());
_fileExists = false;
}
return _fileExists;
}
/// <summary>
/// Deletes the named file if it exists on the FTP target
/// </summary>
/// <param name="sourceFilename">The filename (with no PATH) to delete</param>
/// <param name="address">The FTP server and path</param>
/// <param name="ftpUsername">The user name to connect to the FTP target as</param>
/// <param name="ftpPassword">The password to connect to the FTP target with</param>
/// <returns>True if the named file was deleted successfully</returns>
public static bool FTPDeleteFile(string sourceFilename,
string address,
string ftpUsername,
string ftpPassword)
{
bool _fileDeleted = false;
System.Uri _targetURI = new System.Uri("ftp://" + address + @"/" + sourceFilename);
FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(_targetURI);
req.Credentials = new NetworkCredential(ftpUsername,
ftpPassword);
req.UsePassive = false;
req.KeepAlive = false;
req.Method = WebRequestMethods.Ftp.DeleteFile;
try
{
FtpWebResponse wr = (FtpWebResponse)req.GetResponse();
if (wr.StatusCode == FtpStatusCode.CommandOK
|| wr.StatusCode == FtpStatusCode.ClosingData
|| wr.StatusCode == FtpStatusCode.ClosingControl
|| wr.StatusCode == FtpStatusCode.ConnectionClosed
|| wr.StatusCode == FtpStatusCode.FileActionOK
)
{
_fileDeleted = true;
}
else
{
System.Diagnostics.Trace.TraceError(string.Concat(sourceFilename, " ftp status code: ", wr.StatusCode.ToString()));
_fileDeleted = false;
}
}
catch (Exception ex)
{
// If any error occured then the file effectively doesn't exist
System.Diagnostics.Trace.TraceError(ex.ToString());
_fileDeleted = false;
}
return _fileDeleted;
}
}