示例#1
0
        public static FsPath ToAppRootedPath(this FsPath path)
        {
            if (path.IsPathRooted())
            {
                return(path);
            }

            FsPath result = Root.Join(path);

            return(result);
        }
示例#2
0
        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);
            }
        }
示例#3
0
        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));
        }
示例#4
0
        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);
        }
示例#5
0
        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();
            }
        }
示例#6
0
        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);
        }
示例#7
0
        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));
        }
示例#8
0
 public static FileStream OpenFile(this FsPath path, FileMode mode) =>
 System.IO.File.Open(path.Value, mode);
示例#9
0
 public static FileStream OpenFile(this FsPath path, FileMode mode, FileAccess access, FileShare share) =>
 System.IO.File.Open(path.Value, mode, access, share);
示例#10
0
 public static string Extension(this FsPath path) =>
 Path.GetExtension(path.Value);
示例#11
0
 public static DirectoryInfo CreateDirectory(this FsPath path) =>
 System.IO.Directory.CreateDirectory(path.Value);
示例#12
0
 public static string ReadAllText(this FsPath path, Encoding encoding = null) =>
 System.IO.File.ReadAllText(path.Value, encoding ?? Encoding.UTF8);
示例#13
0
 public static byte[] ReadAllBytes(this FsPath path) =>
 System.IO.File.ReadAllBytes(path.Value);
示例#14
0
 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(_));
示例#15
0
 public static void WriteAllLines(this FsPath path, IEnumerable <string> lines) =>
 System.IO.File.WriteAllLines(path.Value, lines);
示例#16
0
 public static FsPath Or(this FsPath?path, FsPath other) =>
 !path.HasValue || string.IsNullOrEmpty(path.Value.Value)
                         ? other
                         : path.Value;
示例#17
0
 public static FsPath Or(this FsPath path, string other) =>
 string.IsNullOrEmpty(path.Value)
                         ? new FsPath(other)
                         : path;
示例#18
0
 public static bool IsDirectory(this FsPath path) =>
 System.IO.Directory.Exists(path.Value);
示例#19
0
 public static FsPath Or(this FsPath path, FsPath other) =>
 string.IsNullOrEmpty(path.Value)
                         ? other
                         : path;
示例#20
0
 public static bool HasValue(this FsPath path) =>
 !string.IsNullOrEmpty(path.Value);
示例#21
0
 public static StreamReader OpenText(this FsPath path) =>
 System.IO.File.OpenText(path.Value);
示例#22
0
 public static void WriteAllText(this FsPath path, string text) =>
 System.IO.File.WriteAllText(path.Value, text);
示例#23
0
 public static StreamWriter CreateText(this FsPath path) =>
 System.IO.File.CreateText(path.Value);
示例#24
0
 public static void WriteAllBytes(this FsPath path, byte[] bytes) =>
 System.IO.File.WriteAllBytes(path.Value, bytes);
示例#25
0
 public static void WriteToFile(FsPath targetFile, IEnumerable <FileSignature> signatures)
 {
     targetFile.WriteAllLines(signatures.Select(_ => _.Md5Hash + "\t" + _.Path.Value));
 }
示例#26
0
 public static FsPath ChangeDirectory(this FsPath path, FsPath directory) =>
 directory.Join(path.Basename());
示例#27
0
 public static FsPath OrEmpty(this FsPath path) =>
 path.Value == null
                         ? FsPath.Empty
                         : path;
示例#28
0
 public static string[] ReadAllLines(this FsPath path) =>
 System.IO.File.ReadAllLines(path.Value);
示例#29
0
 public bool Compress(FsPath path, FsPath output)
 {
     return(run($"a \"{output}\" -t7z \"{path}\" -r"));
 }
示例#30
0
 public static FsPath ExpandEnvironmentVariables(this FsPath path) =>
 new FsPath(Environment.ExpandEnvironmentVariables(path.Value));