public static bool CopyDirectory(this FsPath sourceDirectory, FsPath TargetDir, ILog log) { try { if (!Directory.Exists(TargetDir.ToString())) { log.Detail("Creating directory: {0}", TargetDir); Directory.CreateDirectory(TargetDir.ToString()); } //Copy all the files & Replaces any files with the same name foreach (string newPath in Directory.GetFiles(sourceDirectory.ToString(), "*.*", SearchOption.AllDirectories)) { var targetfile = newPath.Replace(sourceDirectory.ToString(), TargetDir.ToString()); log.Detail("Copy file: {0} to {1}", newPath, targetfile); var targetDir = Path.GetDirectoryName(targetfile); if (targetDir != null && !Directory.Exists(targetDir)) { log.Detail("Creating directory: {0}", targetDir); Directory.CreateDirectory(targetDir.ToString()); } File.Copy(newPath, targetfile, true); } return(true); } catch (Exception ex) { log.Warning("CopyDirectory failed: {0} to {1}", sourceDirectory, TargetDir); log.Detail(ex.Message); return(false); } }
public static bool Copy(this FsPath source, FsPath target, ILog log) { try { if (!source.IsExisting) { log.Warning("Source doesn't exist, skipping: {0}"); return(false); } var dir = Path.GetDirectoryName(target.ToString()) ?? string.Empty; if (!Directory.Exists(dir)) { log.Detail("Creating directory: {0}", dir); Directory.CreateDirectory(dir); } File.Copy(source.ToString(), target.ToString()); return(true); } catch (Exception ex) { log.Warning("Copy failed: {0} to {1}", source, target); log.Detail(ex.Message); return(false); } }
public static bool WriteFile(this FsPath target, ILog log, params string[] contents) { try { FileInfo fileInfo = new FileInfo(target.ToString()); if (!fileInfo.Exists && fileInfo.Directory != null) { Directory.CreateDirectory(fileInfo.Directory.FullName); } using (var writer = File.CreateText(target.ToString())) { foreach (var content in contents) { writer.Write(content); } } return(true); } catch (Exception ex) { log.Warning("WriteFile failed: {0}", target); log.Detail(ex.Message); return(false); } }
public static FileStream CreateStream(this FsPath target, ILog log) { var dir = Path.GetDirectoryName(target.ToString()) ?? string.Empty; if (!Directory.Exists(dir)) { log.Detail("Creating directory: {0}", dir); Directory.CreateDirectory(dir); } return(File.Create(target.ToString())); }
public static IEnumerable <FsPath> GetAllFiles(this FsPath directory, string mask = "*.*") { foreach (var file in Directory.GetFiles(directory.ToString(), mask, SearchOption.AllDirectories)) { yield return(new FsPath(file)); } }
public static bool CreateBackup(this FsPath source, ILog log) { try { if (!source.IsExisting) { log.Detail("Source doesn't exist, skipping: {0}"); return(false); } string targetname = $"{source}_backup"; if (File.Exists(targetname)) { bool exists = true; int counter = 1; do { targetname = $"{source}_backup{counter}"; ++counter; exists = File.Exists(targetname); }while (exists); } File.Copy(source.ToString(), targetname); return(true); } catch (Exception ex) { log.Warning("CreateBackup failed: {0}", source); log.Detail(ex.Message); return(false); } }
public static string GetSRI(FsPath path) { using (var fs = File.OpenRead(path.ToString())) { using (var hashAlgorithm = new SHA384Managed()) { byte[] hash = hashAlgorithm.ComputeHash(fs); return("sha384-" + Convert.ToBase64String(hash)); } } }
public static string ReadFile(this FsPath path, ILog log) { try { using (var reader = File.OpenText(path.ToString())) { return(reader.ReadToEnd()); } } catch (Exception ex) { log.Warning("ReadFile failed: {0}", path); log.Detail(ex.Message); return(string.Empty); } }
public static bool CreateDir(this FsPath path, ILog log) { try { log.Detail("Creating directory: {0}", path); if (!FsPath.IsEmptyPath(path)) { Directory.CreateDirectory(path.ToString()); return(true); } else { log.Warning("CreateDir called with empty input path"); return(false); } } catch (Exception ex) { log.Warning("CreateDir failed: {0}", path); log.Detail(ex.Message); return(false); } }