Beispiel #1
0
        public Task <IFileStoreEntry> GetDirectoryInfoAsync(string path)
        {
            var physicalPath = GetPhysicalPath(path);

            var directoryInfo = new PhysicalDirectoryInfo(new DirectoryInfo(physicalPath));

            if (directoryInfo.Exists)
            {
                return(Task.FromResult <IFileStoreEntry>(new FileSystemStoreEntry(path, directoryInfo)));
            }

            return(Task.FromResult <IFileStoreEntry>(null));
        }
Beispiel #2
0
        public Task <PhysicalDirectoryInfo> GetPhysicalDirectoryInfo(string directorypath)
        {
            var physicalPath = GetPhysicalPath(directorypath);

            var directoryInfo = new PhysicalDirectoryInfo(new DirectoryInfo(physicalPath));

            if (directoryInfo.Exists)
            {
                return(Task.FromResult(directoryInfo));
            }

            return(Task.FromResult <PhysicalDirectoryInfo>(null));
        }
Beispiel #3
0
        public IFileStoreEntry GetDirectoryInfo(string path)
        {
            var physicalPath = GetPhysicalPath(path);

            var directoryInfo = new PhysicalDirectoryInfo(new DirectoryInfo(physicalPath));

            if (directoryInfo.Exists)
            {
                return(new FileSystemStoreEntry(path, directoryInfo));
            }

            return(null);
        }
        /// <summary>
        /// Enumerate a directory at the given path, if any.
        /// </summary>
        /// <param name="subpath">A path under the root directory</param>
        /// <param name="contents">The discovered directories, if any</param>
        /// <returns>True if a directory was discovered at the given path</returns>
        public bool TryGetDirectoryContents(string subpath, out IEnumerable <IFileInfo> contents)
        {
            //Console.WriteLine("[LOG.FILE] ====> " + subpath);

            try
            {
                if (subpath.StartsWith("/", StringComparison.Ordinal))
                {
                    subpath = subpath.Substring(1);
                }
                var fullPath = GetFullPath(subpath);
                if (fullPath != null)
                {
                    var directoryInfo = new DirectoryInfo(fullPath);
                    if (!directoryInfo.Exists)
                    {
                        contents = null;
                        return(false);
                    }

                    FileSystemInfo[] physicalInfos = directoryInfo.GetFileSystemInfos();
                    var virtualInfos = new IFileInfo[physicalInfos.Length];
                    for (int index = 0; index != physicalInfos.Length; ++index)
                    {
                        var fileInfo = physicalInfos[index] as FileInfo;
                        if (fileInfo != null)
                        {
                            virtualInfos[index] = new PhysicalFileInfo(fileInfo);
                        }
                        else
                        {
                            virtualInfos[index] = new PhysicalDirectoryInfo((DirectoryInfo)physicalInfos[index]);
                        }
                    }
                    contents = virtualInfos;
                    return(true);
                }
            }
            catch (ArgumentException)
            {
            }
            catch (DirectoryNotFoundException)
            {
            }
            catch (IOException)
            {
            }
            contents = null;
            return(false);
        }
Beispiel #5
0
        /// <summary>
        /// 取得目录信息
        /// </summary>
        /// <param name="relativePath">相对路径</param>
        /// <param name="rootPhysicalPath">物理根路径</param>
        /// <returns>目录信息</returns>
        public IFileEntity GetDirectoryInfo(string relativePath, string rootPhysicalPath)
        {
            var path          = GetFilePhysicalPath(relativePath, rootPhysicalPath);
            var directoryInfo = new PhysicalDirectoryInfo(new DirectoryInfo(path));

            return(directoryInfo.Exists ? new FileManageEntity
            {
                FilePhysicalPath = path,
                IsDirectory = directoryInfo.IsDirectory,
                LastModified = directoryInfo.LastModified.UtcDateTime,
                Length = directoryInfo.Length,
                RelativePath = relativePath,
                Name = directoryInfo.Name
            } : null);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="subpath">A path under the root directory</param>
        /// <param name="contents">The discovered directories, if any</param>
        /// <returns>True if a directory was discovered at the given path</returns>
        public bool TryGetDirectoryContents(string subpath, out IEnumerable <IFileInfo> contents)
        {
            try
            {
                var fullPath = GetFullPath(subpath);
                if (fullPath != null)
                {
                    var directoryInfo = new DirectoryInfo(fullPath);
                    if (!directoryInfo.Exists)
                    {
                        contents = null;
                        return(false);
                    }

                    FileSystemInfo[] physicalInfos = directoryInfo.GetFileSystemInfos();
                    var virtualInfos = new IFileInfo[physicalInfos.Length];
                    for (int index = 0; index != physicalInfos.Length; ++index)
                    {
                        var fileInfo = physicalInfos[index] as FileInfo;
                        if (fileInfo != null)
                        {
                            virtualInfos[index] = new PhysicalFileInfo(fileInfo);
                        }
                        else
                        {
                            virtualInfos[index] = new PhysicalDirectoryInfo((DirectoryInfo)physicalInfos[index]);
                        }
                    }
                    contents = virtualInfos;
                    return(true);
                }
            }
            catch (ArgumentException)
            {
            }
            catch (DirectoryNotFoundException)
            {
            }
            catch (IOException)
            {
            }
            contents = null;
            return(false);
        }
        /// <summary>
        /// Creates a directory at the given path.
        /// </summary>
        /// <param name="subpath">A path under the root directory</param>
        /// <returns>
        /// The directory information. Caller must check
        /// <see cref="IFileInfo.Exists"/> property.
        /// </returns>
        public IFileInfo CreateDirectory(string subpath)
        {
            PhysicalDirectoryInfo?physicalDirectoryInfo = null;
            var fileInfo = this.GetDirectoryInfo(subpath);

            if (!(fileInfo is NotFoundFileInfo))
            {
                var fileSystemInfo = new DirectoryInfo(fileInfo.PhysicalPath);
                if (!fileSystemInfo.Exists)
                {
                    fileSystemInfo.Create();
                    fileSystemInfo.Refresh();
                }

                physicalDirectoryInfo = new PhysicalDirectoryInfo(fileSystemInfo);
            }

            return(physicalDirectoryInfo ?? fileInfo);
        }
        public IAsyncEnumerable <IFileStoreEntry> GetDirectoryContentAsync(string path = null, bool includeSubDirectories = false)
        {
            try
            {
                var physicalPath = GetPhysicalPath(path);
                var results      = new List <IFileStoreEntry>();

                if (!Directory.Exists(physicalPath))
                {
                    return(results.ToAsyncEnumerable());
                }

                // Add directories.
                results.AddRange(
                    Directory
                    .GetDirectories(physicalPath, "*", includeSubDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
                    .Select(f =>
                {
                    var fileSystemInfo   = new PhysicalDirectoryInfo(new DirectoryInfo(f));
                    var fileRelativePath = f.Substring(_fileSystemPath.Length);
                    var filePath         = this.NormalizePath(fileRelativePath);
                    return(new FileSystemStoreEntry(filePath, fileSystemInfo));
                }));

                // Add files.
                results.AddRange(
                    Directory
                    .GetFiles(physicalPath, "*", includeSubDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
                    .Select(f =>
                {
                    var fileSystemInfo   = new PhysicalFileInfo(new FileInfo(f));
                    var fileRelativePath = f.Substring(_fileSystemPath.Length);
                    var filePath         = this.NormalizePath(fileRelativePath);
                    return(new FileSystemStoreEntry(filePath, fileSystemInfo));
                }));

                return(results.ToAsyncEnumerable());
            }
            catch (Exception ex)
            {
                throw new FileStoreException($"Cannot get directory content with path '{path}'.", ex);
            }
        }
Beispiel #9
0
        public IList <IFileStoreEntry> GetDirectoryContent(string path = null, bool includeSubDirectories = false, bool listDirectories = true, bool listFiles = true)
        {
            var physicalPath = GetPhysicalPath(path);
            var results      = new List <IFileStoreEntry>();

            if (!Directory.Exists(physicalPath))
            {
                return(results.ToList());
            }

            // Add directories.
            if (listDirectories)
            {
                results.AddRange(
                    Directory
                    .GetDirectories(physicalPath, "*", includeSubDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
                    .Select(f =>
                {
                    var fileSystemInfo   = new PhysicalDirectoryInfo(new DirectoryInfo(f));
                    var fileRelativePath = f.Substring(_fileSystemPath.Length);
                    var filePath         = this.NormalizePath(fileRelativePath);
                    return(new FileSystemStoreEntry(filePath, fileSystemInfo));
                }));
            }

            // Add files.
            if (listFiles)
            {
                results.AddRange(
                    Directory
                    .GetFiles(physicalPath, "*", includeSubDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
                    .Select(f =>
                {
                    var fileSystemInfo   = new PhysicalFileInfo(new FileInfo(f));
                    var fileRelativePath = f.Substring(_fileSystemPath.Length);
                    var filePath         = this.NormalizePath(fileRelativePath);
                    return(new FileSystemStoreEntry(filePath, fileSystemInfo));
                }));
            }

            return(results.ToList());
        }
        public Task <IFileStoreEntry> GetDirectoryInfoAsync(string path)
        {
            try
            {
                var physicalPath = GetPhysicalPath(path);

                var directoryInfo = new PhysicalDirectoryInfo(new DirectoryInfo(physicalPath));

                if (directoryInfo.Exists)
                {
                    return(Task.FromResult <IFileStoreEntry>(new FileSystemStoreEntry(path, directoryInfo)));
                }

                return(Task.FromResult <IFileStoreEntry>(null));
            }
            catch (Exception ex)
            {
                throw new FileStoreException($"Cannot get directory info with path '{path}'.", ex);
            }
        }
Beispiel #11
0
        /// <summary>
        /// 取得目录内容包括目录和文件
        /// </summary>
        /// <param name="relativePath">相对路径</param>
        /// <param name="rootPhysicalPath">物理根路径</param>
        /// <returns>目录内容</returns>
        public IEnumerable <IFileEntity> GetDirectoryContents(string relativePath, string rootPhysicalPath)
        {
            var results       = new List <IFileEntity>();
            var path          = GetFilePhysicalPath(relativePath, rootPhysicalPath);
            var directoryInfo = new PhysicalDirectoryInfo(new DirectoryInfo(path));

            if (!directoryInfo.Exists)
            {
                return(results);
            }

            // 全部目录
            Directory.GetDirectories(path).ToList().ForEach(dir =>
                                                            results.Add(GetDirectoryInfo(dir.Substring(rootPhysicalPath.Length), rootPhysicalPath)));

            // 全部文件
            Directory.GetFiles(path).ToList().ForEach(file =>
                                                      results.Add(GetFileInfo(file.Substring(rootPhysicalPath.Length), rootPhysicalPath)));

            return(results);
        }
Beispiel #12
0
        public Task <IEnumerable <IFileStoreEntry> > GetDirectoryContentAsync(string path = null)
        {
            var physicalPath = GetPhysicalPath(path);

            if (!Directory.Exists(physicalPath))
            {
                throw new FileStoreException($"Cannot get content of directory '{path}' because it does not exist.");
            }

            var results = new List <IFileStoreEntry>();

            // Add directories.
            results.AddRange(
                Directory
                .GetDirectories(physicalPath)
                .Select(f =>
            {
                var fileSystemInfo   = new PhysicalDirectoryInfo(new DirectoryInfo(f));
                var fileRelativePath = f.Substring(_fileSystemPath.Length);
                var filePath         = this.NormalizePath(fileRelativePath);
                return(new FileSystemStoreEntry(filePath, fileSystemInfo));
            }));

            // Add files.
            results.AddRange(
                Directory
                .GetFiles(physicalPath)
                .Select(f =>
            {
                var fileSystemInfo   = new PhysicalFileInfo(new FileInfo(f));
                var fileRelativePath = f.Substring(_fileSystemPath.Length);
                var filePath         = this.NormalizePath(fileRelativePath);
                return(new FileSystemStoreEntry(filePath, fileSystemInfo));
            }));

            return(Task.FromResult((IEnumerable <IFileStoreEntry>)results));
        }
        public Task <IEnumerable <IFileStoreEntry> > GetDirectoryContentAsync(string path = null, bool includeSubDirectories = false)
        {
            var physicalPath = GetPhysicalPath(path);
            var results      = new List <IFileStoreEntry>();

            if (!Directory.Exists(physicalPath))
            {
                return(Task.FromResult((IEnumerable <IFileStoreEntry>)results));
            }

            // Add directories.
            results.AddRange(
                Directory
                .GetDirectories(physicalPath, "*", includeSubDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
                .Select(f =>
            {
                var fileSystemInfo   = new PhysicalDirectoryInfo(new DirectoryInfo(f));
                var fileRelativePath = f.Substring(_fileSystemPath.Length);
                var filePath         = this.NormalizePath(fileRelativePath);
                return(new FileSystemStoreEntry(filePath, fileSystemInfo));
            }));

            // Add files.
            results.AddRange(
                Directory
                .GetFiles(physicalPath, "*", includeSubDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
                .Select(f =>
            {
                var fileSystemInfo   = new PhysicalFileInfo(new FileInfo(f));
                var fileRelativePath = f.Substring(_fileSystemPath.Length);
                var filePath         = this.NormalizePath(fileRelativePath);
                return(new FileSystemStoreEntry(filePath, fileSystemInfo));
            }));

            return(Task.FromResult((IEnumerable <IFileStoreEntry>)results));
        }
 /// <summary>
 /// 构造一个 <see cref="PhysicalStorableDirectoryInfo"/>。
 /// </summary>
 /// <param name="info">给定的 <see cref="PhysicalDirectoryInfo"/>。</param>
 public PhysicalStorableDirectoryInfo(PhysicalDirectoryInfo info)
 {
     _info = info;
 }
        /// <summary>
        /// Enumerate a directory at the given path, if any.
        /// </summary>
        /// <param name="subpath">A path under the root directory</param>
        /// <param name="contents">The discovered directories, if any</param>
        /// <returns>True if a directory was discovered at the given path</returns>
        public bool TryGetDirectoryContents(string subpath, out IEnumerable<IFileInfo> contents)
        {
            try
            {
                if (subpath.StartsWith("/", StringComparison.Ordinal))
                {
                    subpath = subpath.Substring(1);
                }
                var fullPath = GetFullPath(subpath);
                if (fullPath != null)
                {
                    var directoryInfo = new DirectoryInfo(fullPath);
                    if (!directoryInfo.Exists)
                    {
                        contents = null;
                        return false;
                    }

                    FileSystemInfo[] physicalInfos = directoryInfo.GetFileSystemInfos();
                    var virtualInfos = new IFileInfo[physicalInfos.Length];
                    for (int index = 0; index != physicalInfos.Length; ++index)
                    {
                        var fileInfo = physicalInfos[index] as FileInfo;
                        if (fileInfo != null)
                        {
                            virtualInfos[index] = new PhysicalFileInfo(fileInfo);
                        }
                        else
                        {
                            virtualInfos[index] = new PhysicalDirectoryInfo((DirectoryInfo)physicalInfos[index]);
                        }
                    }
                    contents = virtualInfos;
                    return true;
                }
            }
            catch (ArgumentException)
            {
            }
            catch (DirectoryNotFoundException)
            {
            }
            catch (IOException)
            {
            }
            contents = null;
            return false;
        }
 public PhysicalDirectory(string path)
 {
     _directoryInfo = new PhysicalDirectoryInfo(new DirectoryInfo(path));
     _contents      = new PhysicalDirectoryContents(path);
 }