public static FsPath ToAppRootedPath(this FsPath path) { if (path.IsPathRooted()) { return(path); } FsPath result = Root.Join(path); return(result); }
public static void CopyDirectoryTo(this FsPath path, FsPath other, bool overwrite) { foreach (FsPath dirPath in path.EnumerateDirectories("*", SearchOption.AllDirectories).ToArray()) { dirPath.ChangeDirectory(path, other).CreateDirectory(); } foreach (FsPath newPath in path.EnumerateFiles("*", SearchOption.AllDirectories)) { newPath.CopyFileTo(newPath.ChangeDirectory(path, other), overwrite); } }
public static FsPath WithName(this FsPath path, Func <string, string> transformation) { var tail = path.Basename(); string transformed = transformation(tail); if (FsPath.Comparer.Equals(tail, transformed)) { return(path); } return(path.Parent().Join(transformed)); }
public static FsPath Deserialize(string value) { if (Path.DirectorySeparatorChar != '\\') { value = value.Replace('\\', Path.DirectorySeparatorChar); } var result = new FsPath(value); foreach (var substitution in _substitutions) { result = substitution(result); } return(result); }
public static void EnsureEmptyDirectory(this FsPath path) { if (!path.IsDirectory()) { path.CreateDirectory(); return; } foreach (var subdirInfo in path.EnumerateDirectories().ToArray()) { subdirInfo.DeleteDirectory(recursive: true); } foreach (var fileInfo in path.EnumerateFiles().ToArray()) { fileInfo.DeleteFile(); } }
public static FileSignature[] ReadFromFile(FsPath targetFile, bool internPath) { if (!targetFile.IsFile()) { return(null); } var lines = targetFile.ReadAllLines() .Where(_ => !string.IsNullOrEmpty(_)); var result = lines .Select(_ => new FileSignature { Md5Hash = _.Substring(0, 32), Path = FsPathPersistence.Deserialize(_.Substring(33)).Intern(internPath) }).ToArray(); return(result); }
public bool Extract(FsPath archive, FsPath targetDirectory, IEnumerable <FsPath> excludedFiles = null) { excludedFiles ??= Enumerable.Empty <FsPath>(); var argsBuilder = new StringBuilder($"x -aoa \"{archive}\" \"-o{targetDirectory}\""); foreach (FsPath excludedFile in excludedFiles.Append(_executable)) { var relativePath = excludedFile.RelativeTo(targetDirectory); if (relativePath == FsPath.None) { continue; } argsBuilder.Append($" \"-x!{relativePath}\""); } string args = argsBuilder.ToString(); return(run(args)); }
public static FileStream OpenFile(this FsPath path, FileMode mode) => System.IO.File.Open(path.Value, mode);
public static FileStream OpenFile(this FsPath path, FileMode mode, FileAccess access, FileShare share) => System.IO.File.Open(path.Value, mode, access, share);
public static string Extension(this FsPath path) => Path.GetExtension(path.Value);
public static DirectoryInfo CreateDirectory(this FsPath path) => System.IO.Directory.CreateDirectory(path.Value);
public static string ReadAllText(this FsPath path, Encoding encoding = null) => System.IO.File.ReadAllText(path.Value, encoding ?? Encoding.UTF8);
public static byte[] ReadAllBytes(this FsPath path) => System.IO.File.ReadAllBytes(path.Value);
public static IEnumerable <FsPath> EnumerateChildren( this FsPath path, string pattern = "*", SearchOption option = SearchOption.TopDirectoryOnly) => System.IO.Directory.EnumerateFileSystemEntries(path.Value, pattern, option).Select(_ => new FsPath(_));
public static void WriteAllLines(this FsPath path, IEnumerable <string> lines) => System.IO.File.WriteAllLines(path.Value, lines);
public static FsPath Or(this FsPath?path, FsPath other) => !path.HasValue || string.IsNullOrEmpty(path.Value.Value) ? other : path.Value;
public static FsPath Or(this FsPath path, string other) => string.IsNullOrEmpty(path.Value) ? new FsPath(other) : path;
public static bool IsDirectory(this FsPath path) => System.IO.Directory.Exists(path.Value);
public static FsPath Or(this FsPath path, FsPath other) => string.IsNullOrEmpty(path.Value) ? other : path;
public static bool HasValue(this FsPath path) => !string.IsNullOrEmpty(path.Value);
public static StreamReader OpenText(this FsPath path) => System.IO.File.OpenText(path.Value);
public static void WriteAllText(this FsPath path, string text) => System.IO.File.WriteAllText(path.Value, text);
public static StreamWriter CreateText(this FsPath path) => System.IO.File.CreateText(path.Value);
public static void WriteAllBytes(this FsPath path, byte[] bytes) => System.IO.File.WriteAllBytes(path.Value, bytes);
public static void WriteToFile(FsPath targetFile, IEnumerable <FileSignature> signatures) { targetFile.WriteAllLines(signatures.Select(_ => _.Md5Hash + "\t" + _.Path.Value)); }
public static FsPath ChangeDirectory(this FsPath path, FsPath directory) => directory.Join(path.Basename());
public static FsPath OrEmpty(this FsPath path) => path.Value == null ? FsPath.Empty : path;
public static string[] ReadAllLines(this FsPath path) => System.IO.File.ReadAllLines(path.Value);
public bool Compress(FsPath path, FsPath output) { return(run($"a \"{output}\" -t7z \"{path}\" -r")); }
public static FsPath ExpandEnvironmentVariables(this FsPath path) => new FsPath(Environment.ExpandEnvironmentVariables(path.Value));