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; } }




Reply With Quote