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




Reply With Quote