GetFileSystemInfos() 공개 추상적인 메소드

public abstract GetFileSystemInfos ( ) : System.IO.Abstractions.FileSystemInfoBase[]
리턴 System.IO.Abstractions.FileSystemInfoBase[]
예제 #1
0
        static void ListDirectory(DirectoryInfoBase dir)
        {
            FileSystemInfoBase[] children = dir.GetFileSystemInfos();

            Console.WriteLine();
            Console.WriteLine(" Directory of {0}", dir.FullName.TrimEnd('\\'));
            Console.WriteLine();

            foreach (DirectoryInfoBase info in children.Where(d => d is DirectoryInfoBase))
            {
                Console.WriteLine(String.Format("{0}    <DIR>          {1}", ToDisplayString(info.LastWriteTime), info.Name));
            }

            int count = 0;
            long total = 0;
            foreach (FileInfoBase info in children.Where(d => !(d is DirectoryInfoBase)))
            {
                FileInfoBase file = (FileInfoBase)info;
                Console.WriteLine(String.Format("{0} {1,17} {2}", ToDisplayString(info.LastWriteTime), file.Length.ToString("#,##0"), info.Name));
                total += file.Length;
                ++count;
            }

            Console.WriteLine(String.Format("{0,16} File(s) {1,14} bytes", count.ToString("#,##0"), total.ToString("#,##0")));
        }
예제 #2
0
        protected override Task<HttpResponseMessage> CreateDirectoryGetResponse(DirectoryInfoBase info, string localFilePath)
        {
            HttpResponseMessage response = Request.CreateResponse();
            var ms = new MemoryStream();
            using (var zip = new ZipArchive(ms, ZipArchiveMode.Create, leaveOpen: true))
            {
                foreach (FileSystemInfoBase fileSysInfo in info.GetFileSystemInfos())
                {
                    var directoryInfo = fileSysInfo as DirectoryInfoBase;
                    if (directoryInfo != null)
                    {
                        zip.AddDirectory(directoryInfo, fileSysInfo.Name);
                    }
                    else
                    {
                        // Add it at the root of the zip
                        zip.AddFile(fileSysInfo.FullName, String.Empty);
                    }
                }
            }

            ms.Seek(0, SeekOrigin.Begin);
            response.Content = new StreamContent(ms);
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");

            // Name the zip after the folder. e.g. "c:\foo\bar\" --> "bar"
            response.Content.Headers.ContentDisposition.FileName = Path.GetFileName(Path.GetDirectoryName(localFilePath)) + ".zip";
            return Task.FromResult(response);
        }
예제 #3
0
 static void CopyInternal(IFileSystem srcFileSystem, DirectoryInfoBase srcFileInfo, IFileSystem dstFileSystem, DirectoryInfoBase dstFileInfo)
 {
     foreach (FileSystemInfoBase src in srcFileInfo.GetFileSystemInfos())
     {
         if (src is FileInfoBase)
         {
             var dst = dstFileSystem.FileInfo.FromFileName(Path.Combine(dstFileInfo.FullName, src.Name));
             _semaphore.WaitOne();
             _tasks.Add(CopyInternalAsync(srcFileSystem, (FileInfoBase)src, dstFileSystem, (FileInfoBase)dst).Finally(() => _semaphore.Release()));
         }
         else
         {
             // recursive
             var dst = dstFileSystem.DirectoryInfo.FromDirectoryName(Path.Combine(dstFileInfo.FullName, src.Name));
             CopyInternal(srcFileSystem, (DirectoryInfoBase)src, dstFileSystem, (DirectoryInfoBase)dst);
         }
     }
 }
예제 #4
0
 protected override Task<HttpResponseMessage> CreateDirectoryGetResponse(DirectoryInfoBase info, string localFilePath)
 {
     HttpResponseMessage response = Request.CreateResponse();
     response.Content = ZipStreamContent.Create(Path.GetFileName(Path.GetDirectoryName(localFilePath)) + ".zip", Tracer, zip =>
     {
         foreach (FileSystemInfoBase fileSysInfo in info.GetFileSystemInfos())
         {
             var directoryInfo = fileSysInfo as DirectoryInfoBase;
             if (directoryInfo != null)
             {
                 zip.AddDirectory(directoryInfo, Tracer, fileSysInfo.Name);
             }
             else
             {
                 // Add it at the root of the zip
                 zip.AddFile(fileSysInfo.FullName, Tracer, String.Empty);
             }
         }
     });
     return Task.FromResult(response);
 }
예제 #5
0
        public static void AddDirectory(this ZipArchive zipArchive, DirectoryInfoBase directory, string directoryNameInArchive)
        {
            bool any = false;
            foreach (var info in directory.GetFileSystemInfos())
            {
                any = true;
                var subDirectoryInfo = info as DirectoryInfoBase;
                if (subDirectoryInfo != null)
                {
                    string childName = Path.Combine(directoryNameInArchive, subDirectoryInfo.Name);
                    zipArchive.AddDirectory(subDirectoryInfo, childName);
                }
                else
                {
                    zipArchive.AddFile((FileInfoBase)info, directoryNameInArchive);
                }
            }

            if (!any)
            {
                // If the directory did not have any files or folders, add a entry for it
                zipArchive.CreateEntry(EnsureTrailingSlash(directoryNameInArchive));
            }
        }
예제 #6
0
 private static void DeleteDirectoryContentsSafe(DirectoryInfoBase directoryInfo, bool ignoreErrors)
 {
     try
     {
         if (directoryInfo.Exists)
         {
             foreach (var fsi in directoryInfo.GetFileSystemInfos())
             {
                 DeleteFileSystemInfo(fsi, ignoreErrors);
             }
         }
     }
     catch
     {
         if (!ignoreErrors) throw;
     }
 }
예제 #7
0
 protected virtual Task<HttpResponseMessage> CreateDirectoryGetResponse(DirectoryInfoBase info, string localFilePath)
 {
     Contract.Assert(info != null);
     try
     {
         // Enumerate directory
         IEnumerable<VfsStatEntry> directory = GetDirectoryResponse(info.GetFileSystemInfos());
         HttpResponseMessage successDirectoryResponse = Request.CreateResponse<IEnumerable<VfsStatEntry>>(HttpStatusCode.OK, directory);
         return Task.FromResult(successDirectoryResponse);
     }
     catch (Exception e)
     {
         Tracer.TraceError(e);
         HttpResponseMessage errorResponse = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e.Message);
         return Task.FromResult(errorResponse);
     }
 }
예제 #8
0
 private static void DeleteDirectoryContentsSafe(DirectoryInfoBase directoryInfo)
 {
     try
     {
         if (directoryInfo.Exists)
         {
             foreach (var fsi in directoryInfo.GetFileSystemInfos())
             {
                 DeleteFileSystemInfo(fsi);
             }
         }
     }
     catch
     {
     }
 }
예제 #9
0
        private IEnumerable<VfsStatEntry> GetDirectoryResponse(DirectoryInfoBase info)
        {
            Contract.Assert(info != null);
            string baseAddress = Request.RequestUri.AbsoluteUri;
            foreach (FileSystemInfoBase fileSysInfo in info.GetFileSystemInfos())
            {
                bool isDirectory = (fileSysInfo.Attributes & FileAttributes.Directory) != 0;
                string mime = isDirectory ? _directoryMediaType.ToString() : MediaTypeMap.GetMediaType(fileSysInfo.Extension).ToString();
                string unescapedHref = isDirectory ? fileSysInfo.Name + UriSegmentSeparator : fileSysInfo.Name;
                long size = isDirectory ? 0 : ((FileInfoBase)fileSysInfo).Length;

                yield return new VfsStatEntry
                {
                    Name = fileSysInfo.Name,
                    MTime = fileSysInfo.LastWriteTimeUtc,
                    Mime = mime,
                    Size = size,
                    Href = baseAddress + Uri.EscapeUriString(unescapedHref),
                };
            }
        }