public abstract GetFileSystemInfos ( ) : System.IO.Abstractions.FileSystemInfoBase[] | ||
Результат | System.IO.Abstractions.FileSystemInfoBase[] |
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"))); }
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); }
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); } } }
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); }
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)); } }
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; } }
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); } }
private static void DeleteDirectoryContentsSafe(DirectoryInfoBase directoryInfo) { try { if (directoryInfo.Exists) { foreach (var fsi in directoryInfo.GetFileSystemInfos()) { DeleteFileSystemInfo(fsi); } } } catch { } }
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), }; } }