简体   繁体   English

从列表中获取父母列表?

[英]Getting a list of parents from a List?

I'm writing a little C# program to scan a directory recursively and get the filesize of each file, a few other bits of information, and add it all to tables in a sqlite database. 我正在编写一个C#程序,以递归方式扫描目录,并获取每个文件的文件大小以及其他一些信息,并将其全部添加到sqlite数据库的表中。 I've hit a bit of a snag, however - I want to get the directory data (Table structure below) and turn it into a List< Class > the Class being a class with the file data, directory data and a List of all of the directory's parents in order (Parent, grand parent, etc.). 但是,我遇到了一些麻烦-我想获取目录数据(下面的表结构)并将其转换为List <Class>,该Class是包含文件数据,目录数据所有列表的类目录父级的顺序(父级,祖父母等)。

Directory table: 目录表:

id int
parentID int
directoryName string

How would I go about doing this? 我将如何去做呢? Getting a list of children is simple, but I'm finding that getting a list of parents is fairly difficult without repeatedly looping through the directory list. 获取孩子列表很简单,但是我发现要获取父母列表非常困难,而不必反复遍历目录列表。

I think that List<Class> is not the right class to use. 我认为List<Class>是不适合使用的类。

Depending on what you are going to do with your list, I would use a Dictionary<int, Class> if the order of your list is not important, otherwise SortedDictionary<int, Class> if you want it to be ordered by ID, or OrderedDictionary if you want to be able to specify a different way of sorting. 根据列表的处理方式,如果列表的顺序不重要,我将使用Dictionary<int, Class>如果希望按ID排序,则使用SortedDictionary<int, Class> ;或者如果要能够指定其他排序方式,请使用OrderedDictionary

You can use DirectoryInfo.GetParent() 您可以使用DirectoryInfo.GetParent()

List<DirectoryInfo> parents = new List<DirectoryInfo>();
DirectoryInfo parent = di.Parent;
while(parent != null){
      parents.Add(parent);
      parent = di.Parent;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM