public void LogMessage(string message, params object[] args) { if (Log != null) { Log.LogMessage(message, args); } }
public static void CopyDirectory(string source, string destination, bool recursive, ReportExecutionLog log = null, string searchPattern = "*") { if (!Directory.Exists(destination)) Directory.CreateDirectory(destination); foreach (string file in Directory.GetFiles(source, searchPattern)) { try { var destinationFile = Path.Combine(destination, Path.GetFileName(file)); if (log != null) log.LogMessage("Copy '{0}' to '{1}'", file, destinationFile); File.Copy(file, destinationFile, true); } catch (Exception ex) { Debug.WriteLine(ex.Message); } } if (recursive) { foreach (string directory in Directory.GetDirectories(source)) { CopyDirectory(directory, Path.Combine(destination, Path.GetFileName(directory)), recursive, log, searchPattern); } } }
/// <summary> /// Put directories and file to an FTP Server /// </summary> public void FtpPutDirectory(string source, string destination, bool recursive, ReportExecutionLog log, CustomFtpGetRequest ftpGetRequest, string searchPattern = "*") { if (ftpGetRequest == null) { ftpGetRequest = FtpGetRequest; } if (!FtpDirectoryExists(destination, ftpGetRequest)) { if (log != null) { log.LogMessage("Creating remote directory: " + destination); } var request = ftpGetRequest(destination, WebRequestMethods.Ftp.MakeDirectory); var response = request.GetResponse(); response.Close(); } foreach (string file in Directory.GetFiles(source, searchPattern)) { try { var destinationFile = Path.Combine(destination, Path.GetFileName(file)).Replace("\\", "/"); if (log != null) { log.LogMessage("Copy '{0}' to '{1}'", file, destinationFile); } FtpPutFile(file, destinationFile, ftpGetRequest); } catch (Exception ex) { if (log != null) { log.LogMessage(ex.Message); } } } if (recursive) { foreach (string directory in Directory.GetDirectories(source)) { FtpPutDirectory(directory, Path.Combine(destination, Path.GetFileName(directory)).Replace("\\", "/"), recursive, log, ftpGetRequest, searchPattern); } } }
public static void FtpCopyDirectory(FtpClient client, string source, string destination, bool recursive, ReportExecutionLog log = null) { if (log != null) { log.LogMessage("Copying directory '{0}' to '{1}'", source, destination); } if (!destination.EndsWith("/")) { destination += "/"; } if (recursive) { foreach (var folder in Directory.GetDirectories(source)) { var dest = destination + Path.GetFileName(folder); if (log != null) { log.LogMessage("Copying directory '{0}' to '{1}'", folder, dest); } var results = client.UploadDirectory(folder, dest, FtpFolderSyncMode.Update, FtpRemoteExists.Overwrite); if (log != null) { foreach (var result in results.Where(i => i.IsSuccess)) { log.LogMessage("'{0}'", result.RemotePath); } foreach (var result in results.Where(i => i.Exception != null)) { log.LogMessage("'{0}' Error: {1}", result.RemotePath, result.Exception.Message + (result.Exception.InnerException != null ? " " + result.Exception.InnerException.Message : "")); } } } } foreach (var file in Directory.GetFiles(source)) { var dest = destination + Path.GetFileName(file); if (log != null) { log.LogMessage("Copying file '{0}' to '{1}'", file, dest); } try { client.UploadFile(file, dest); } catch (Exception ex) { if (log != null) { log.LogMessage("'{0}' Error: '{1}'", file, ex.Message + (ex.InnerException != null ? " " + ex.InnerException.Message : "")); } } } }