I am trying to get each folder , sub folders and files into an object out of a path.
This path will have many top level folder with many sub folders and files.
I need to process each top folder and its sub folders and files separately.
example
C:\\temp\\Folder1\\SubFolder\\SubFiles
C:\\temp\\Folder2\\SubFiles\\SubFolders\\SubFiles
Need to process Folder1 with its sub directories and enclose it into an object. and same for folder 2.
here is some code , getting access denied because the stream remains open and if I wrap the stream in a using , its closed before I can process the information.
private static IEnumerable<LocalFile> GetLocalFile(string source)
{
return Directory.GetDirectories(source)
.SelectMany(m =>
{
return Directory.EnumerateFileSystemEntries(m, "*", SearchOption.AllDirectories)
.Select(x =>
{
var relative = x.Substring(source.Length + 1);
var localFile = new LocalFile(relative,
() =>
{
return File.OpenRead(x);
});
return localFile;
});
});
}
public sealed class LocalFile
{
private readonly Func<Task<Stream>> openLocal;
public LocalFile(string relativePath, Func<Stream> openLocal)
{
if (openLocal == null)
{
throw new ArgumentNullException(nameof(openLocal));
}
this.RelativePath = relativePath ?? throw new ArgumentNullException(nameof(relativePath));
this.openLocal = () => Task.FromResult(openLocal());
}
public LocalFile(string relativePath, Func<Task<Stream>> openLocal)
{
this.RelativePath = relativePath ?? throw new ArgumentNullException(nameof(relativePath));
this.openLocal = openLocal ?? throw new ArgumentNullException(nameof(openLocal));
}
public string RelativePath { get; }
public Task<Stream> OpenLocal() => this.openLocal();
}
Thank you
Finally got it...
Made a new class
public class LocalFolder
{
public string FolderPath { get; }
public IEnumerable<LocalFile> FolderFiles { get; }
public LocalFolder(string folderPath, IEnumerable<LocalFile> files)
{
this.FolderPath = folderPath ?? throw new ArgumentNullException(nameof(folderPath));
this.FolderFiles = files ?? throw new ArgumentNullException(nameof(files));
}
}
here is the code
private static IEnumerable<LocalFolder> GetFolderFiles(string source)
{
var directories = Directory.EnumerateFileSystemEntries(source);
var folderFiles = directories
.Where(d => !d.Contains("."))
.Select(m =>
{
var files = Directory.EnumerateFiles(m, "*", SearchOption.AllDirectories)
.Select(f =>
{
var relative = f.Substring(source.Length + 1);
return new LocalFile(relative, () => File.OpenRead(f));
});
return new LocalFolder(m,
files);
});
var filesinDir = directories
.Where(d => d.Contains("."))
.Select(m =>
{
var files = Directory.EnumerateFiles(source, "*", SearchOption.TopDirectoryOnly)
.Select(f =>
{
var relative = f.Substring(source.Length + 1);
return new LocalFile(relative, () => File.OpenRead(f));
});
return new LocalFolder(m,
files);
});
return folderFiles.Concat(filesinDir);
}
I hope this helps someone whom encounters a similar situation.
Thank you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.