public class Drives : List<Drive> {
public Drives() {
this.AddRange(DriveInfo.GetDrives().Where(p => p.IsReady).Select(p => new Drive(p)));
}
}
public interface Subject {
string Name { get; set; }
string Path { get; set; }
}
public class File : Subject {
public FileInfo Info { get; set; }
public string Name { get; set; }
public string Path { get; set; }
public File(string arg) : this(new FileInfo(arg)) { }
public File(FileInfo arg) {
this.Path = arg.FullName;
this.Name = System.IO.Path.GetFileName(this.Path);
}
}
public abstract class FileGroup : Subject {
public string Name { get; set; }
public string Path { get; set; }
public FileGroup() { }
public FileGroup(string name) { this.Name = name; }
public IEnumerable<Subject> Folders {
get { return LoadChildren().OfType<Folder>(); }
}
public IEnumerable<Subject> Children {
get { return LoadChildren(); }
}
protected virtual IEnumerable<Subject> LoadChildren() {
if (children == null) {
var target = new DirectoryInfo(Path);
Func<FileSystemInfo, bool> filter = p => !(
p.Attributes.HasFlag(FileAttributes.System)
[解决办法]
p.Attributes.HasFlag(FileAttributes.Hidden)
);
var q = target.GetDirectories()
.Where(filter)
.Select(p => new Folder(p as DirectoryInfo)).Concat(
target.GetFiles()
.Where(filter)
.Select(p => new File(p as FileInfo))
as IEnumerable<Subject>
);
return q;
}
return children;
}
protected IEnumerable<Subject> children = null;
}
public class Drive : FileGroup {
public DriveInfo Info { get; set; }
public Drive(DriveInfo arg) {
this.Info = arg;
this.Path = arg.RootDirectory.Name;
this.Name = string.Format("{0} ({1})", arg.VolumeLabel, arg.Name);
}
}
public class Folder : FileGroup {
public DirectoryInfo Info { get; set; }
public Folder(string arg) : this(new DirectoryInfo(arg)) { }
public Folder(DirectoryInfo arg) {
this.Info = arg;
this.Path = arg.FullName;
this.Name = arg.Name;
}
}