示例#1
0
        public static IOResource From(string value, bool strict = true)
        {
            var resource = FileResource.From(value, strict: false) ?? FolderResource.From(value, strict: false) as IOResource;

            ExpectNot(strict && resource == null, "Value is not an I/O resource");

            return(resource);
        }
示例#2
0
        public static bool TryFrom(string folder, string name, out FileResource resource, bool extensionOptional = false)
        {
            resource = FileName.TryFrom(name, out var parsedName, extensionOptional)
        ? new FileResource(FolderResource.From(folder), parsedName)
        : null;

            return(resource != null);
        }
示例#3
0
        private IEnumerable <FolderLink> ReadFolderLinksCore(FolderResource subfolder, bool recursive = false)
        {
            var folderPath = Link.Then(subfolder).ToString();
            var pattern    = "*.*";
            var option     = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;

            return(Directory.GetDirectories(folderPath, pattern, option).Select(subdirectory => FolderLink.From(subdirectory)));
        }
示例#4
0
        public static FileResource From(FolderResource folder, string name, bool extensionOptional = false)
        {
            if (!TryFrom(folder, name, out var resource, extensionOptional))
            {
                throw new FormatException($"Failed to parse file name: {name}");
            }

            return(resource);
        }
示例#5
0
        //
        // Subfolder reads
        //

        public Many <IOResource> ReadFolder(FolderResource subfolder, bool recursive = false)
        {
            return(ReadLinksCore(subfolder, recursive)
                   .Select(link =>
            {
                return link is FolderLink
                                                ? ((FolderLink)link).RelativeTo(Link)
                                                : ((FileLink)link).RelativeTo(Link) as IOResource;
            })
                   .ToMany());
        }
示例#6
0
        public void Write(FolderResource folder, bool overwrite = true)
        {
            var folderPath = Link.Then(folder).ToString();

            if (Directory.Exists(folderPath))
            {
                return;
            }

            Expect(overwrite, "Folder exists and overwrite option is not set");

            Directory.CreateDirectory(folderPath);
        }
示例#7
0
        public static bool TryFrom(string value, out IOResource resource)
        {
            if (FileResource.TryFrom(value, out var file))
            {
                resource = file;
            }
            else
            {
                resource = FolderResource.From(value);
            }

            return(resource != null);
        }
示例#8
0
        public void Delete(FolderResource folder, bool strict = true)
        {
            var folderPath = Link.Then(folder).ToString();

            if (Directory.Exists(folderPath))
            {
                Directory.Delete(folderPath, recursive: true);
            }
            else
            {
                if (strict)
                {
                    ExpectNot(strict, "Folder not found");
                }
            }
        }
示例#9
0
        public static FolderLink FromLocal(string value, bool strict = true)
        {
            var parsedFolder = FolderResource.From(value, strict);

            if (parsedFolder != null && parsedFolder.Path.Segments.Count > 0)
            {
                var path = parsedFolder.Path.Segments.Count == 1
                                        ? LinkPath.Root
                                        : LinkPath.From(parsedFolder.Path.Segments.Skip(1));

                return(new FolderLink(parsedFolder.Path.Segments[0], FolderResource.From(path)));
            }

            Expect(strict, "Cannot parse folder link: " + value);

            return(null);
        }
示例#10
0
        public static bool TryFromLocal(string value, out FolderLink link)
        {
            link = null;

            var path = FolderResource.From(value).Path;

            if (path.Segments.Any())
            {
                var root = path.Segments[0];

                var resource = path.Segments.Count == 1
          ? LinkPath.Root
          : LinkPath.From(path.Segments.Skip(1));

                link = new FolderLink(root, FolderResource.From(resource));
            }

            return(link != null);
        }
示例#11
0
        public static bool TryFromUnc(string value, out FolderLink link)
        {
            link = null;

            if (!String.IsNullOrEmpty(value) && HasUncPrefix(value))
            {
                var path = FolderResource.From(value.Substring(2)).Path;

                var root = $@"\\{path.Segments[0]}";

                var resource = path.Segments.Count == 1
          ? LinkPath.Root
          : LinkPath.From(path.Segments.Skip(1));

                link = new FolderLink(root, FolderResource.From(resource), isUnc: true);
            }

            return(link != null);
        }
示例#12
0
        public static FolderLink FromUnc(string value, bool strict = true)
        {
            if (!String.IsNullOrEmpty(value) && value.StartsWith(@"\\"))
            {
                var parsedFolder = FolderResource.From(value.Substring(2), strict);

                if (parsedFolder != null)
                {
                    var root = @"\\" + parsedFolder.Path.Segments[0].ToString();

                    var path = parsedFolder.Path.Segments.Count == 1
                                                ? LinkPath.Root
                                                : LinkPath.From(parsedFolder.Path.Segments.Skip(1));

                    return(new FolderLink(root, FolderResource.From(path), isUnc: true));
                }
            }

            ExpectNot(strict, "Cannot parse UNC link: " + value);

            return(null);
        }
示例#13
0
        public static bool TryFrom(string value, out FileResource resource, bool extensionOptional = false)
        {
            resource = null;

            var parsedFolder = FolderResource.From(value);

            var file = parsedFolder.Path.Segments.LastOrDefault()?.ToString();

            if (file != null)
            {
                if (FileName.TryFrom(file, out var parsedFile, extensionOptional))
                {
                    if (!parsedFolder.TryUp(out var folderUp))
                    {
                        folderUp = parsedFolder;
                    }

                    resource = new FileResource(folderUp, parsedFile);
                }
            }

            return(resource != null);
        }
示例#14
0
        public new static FileResource From(string value, bool strict = true)
        {
            var parsedFolder = FolderResource.From(value, strict);

            if (parsedFolder != null)
            {
                var fileSegment = parsedFolder.Path.Segments.LastOrDefault();

                if (fileSegment != null)
                {
                    var parsedName = FileName.From(fileSegment.ToString(), strict);

                    if (parsedName != null)
                    {
                        return(new FileResource(parsedFolder.Up(strict: false), parsedName));
                    }
                }
            }

            ExpectNot(strict, "Cannot parse file resource");

            return(null);
        }
示例#15
0
 public bool FolderExists(FolderResource subfolder)
 {
     return(Directory.Exists(Link.Then(subfolder).ToString()));
 }
示例#16
0
 public void Move(FolderResource sourceFolder, FolderResource destinationFolder)
 {
     Directory.Move(Link.Then(sourceFolder).ToString(), Link.Then(destinationFolder).ToString());
 }
示例#17
0
        public static FileResource From(FolderResource folder, string name, bool strict = true)
        {
            var parsedName = FileName.From(name, strict);

            return(parsedName == null ? null : new FileResource(folder, parsedName));
        }
示例#18
0
 public FolderLink Then(FolderResource folder) =>
 new FolderLink(Root, Resource.Then(folder));
示例#19
0
        //
        // Factory
        //

        public static FileResource From(FolderResource folder, FileName name)
        {
            return(new FileResource(folder, name));
        }
示例#20
0
        public static FileResource From(string folder, string name, bool strict = true)
        {
            var parsedFolder = FolderResource.From(folder, strict);

            return(parsedFolder == null ? null : From(parsedFolder, name, strict));
        }
示例#21
0
 public Many <FolderResource> ReadFolders(FolderResource subfolder, bool recursive = false)
 {
     return(ReadFolderLinksCore(subfolder, recursive).Select(folderLink => folderLink.RelativeTo(Link)).ToMany());
 }
示例#22
0
 public static FileResource From(string folder, FileName name) =>
 From(FolderResource.From(folder), name);
示例#23
0
 public static FileResource From(FolderResource folder, FileName name) =>
 new FileResource(folder, name);
示例#24
0
 public static HttpResource From(FolderResource folder) =>
 From(folder.ToString(altSlash: true));
示例#25
0
 public IFolder Then(FolderResource folder)
 {
     return(new LocalFolder(Link.Then(folder)));
 }
示例#26
0
 FileResource(FolderResource folder, FileName name)
 {
     Folder = folder;
     Name   = name;
 }
示例#27
0
 public Many <FolderLink> ReadFolderLinks(FolderResource subfolder, bool recursive = false)
 {
     return(ReadFolderLinksCore(subfolder, recursive).ToMany());
 }
示例#28
0
 public FileResource RelativeTo(FolderResource folder) =>
 new FileResource(Folder.RelativeTo(folder), Name);
示例#29
0
        public static bool TryFrom(LinkText root, string resource, out FolderLink link)
        {
            link = From(root, FolderResource.From(resource));

            return(true);
        }
示例#30
0
 public FileResource RelativeTo(FolderResource folder)
 {
     return(new FileResource(Folder.RelativeTo(folder), Name));
 }