C# 如何使用路径列表创建层次结构?
我在玩Dropbox的Delta API,当我调用Delta方法时,我会得到自上次调用以来更改的路径列表C# 如何使用路径列表创建层次结构?,c#,dropbox,dropbox-api,C#,Dropbox,Dropbox Api,我在玩Dropbox的Delta API,当我调用Delta方法时,我会得到自上次调用以来更改的路径列表 /photos /public /photos/sample album /photos/sample album/boston city flow.jpg /photos/sample album/pensive parakeet.jpg /photos/sample album/costa rican frog.jpg /getting started.pdf /photo
/photos
/public
/photos/sample album
/photos/sample album/boston city flow.jpg
/photos/sample album/pensive parakeet.jpg
/photos/sample album/costa rican frog.jpg
/getting started.pdf
/photos/how to use the photos folder.txt
/public/how to use the public folder.txt
/ies eai.pptx
/documents
/documents/windows phone toolkit in depth 2nd edition.pdf
/prashant
/prashant/iphone indexed list.bmml
/photos/flower.jpg
/photos/trs
/photo.jpg
/hello1
/hello1/new
我很难通过操纵字符串来创建层次结构(在下面提到的类中),有人能给我一个实现的方法/想法吗
public class DeltaItem
{
private List<DeltaItem> _items;
public string Path { get; set; }
public bool IsDir { get; set; }
public List<DeltaItem> Items
{
get
{
return _items ?? (_items = new List<DeltaItem>());
}
}
}
公共类三角洲
{
私人物品清单;
公共字符串路径{get;set;}
公共bool IsDir{get;set;}
公共清单项目
{
得到
{
返回_项???(_项=新列表());
}
}
}
这是一个非常简单的解析操作。首先,我要这样定义类:
public class Node
{
private readonly IDictionary<string, Node> _nodes =
new Dictionary<string, Node>();
public string Path { get; set; }
}
这将为您提供所需的层次结构。您可以公开在字典上工作的操作,这将允许您遍历字典,但这是填充您将使用的一般结构的方式
请注意,您可以从一个没有路径的单一节点开始,然后遍历上面的列表,并对上面列表中的每个项目调用
AddPath
。@casperOne解决方案很好,但只有使用
char[] charSeparators = new char[] {'/'};
不如
char[] charSeparators = new char[] {'\\'};
看起来你干得不错。您还需要什么?for Java在“examples/SearchCache”中包含了一个示例,该示例演示了如何将结果从
/delta
加载到树结构中。
char[] charSeparators = new char[] {'\\'};