如何创建带路径的目录层次结构? [英] How to create hierarchical structure with list of path?

查看:143
本文介绍了如何创建带路径的目录层次结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我玩Dropbox的的三角洲API,当我打电话三角洲方法,我得到自上次调用改变路径的列表。

  /照片
/公共
/照片/样本画册
/照片/样本画册/波士顿市区flow.jpg
/照片/样本画册/沉思parakeet.jpg
/照片/样本画册/哥斯达黎加frog.jpg
/获取started.pdf
/照片/如何使用照片folder.txt
/公/如何使用公共folder.txt
/ IES eai.pptx深入第二edition.pdf

/文件
/文档/ Windows Phone的工具包/ PRASHANT
/ PRASHANT / iphone索引list.bmml
/photos/flower.jpg
/照片/ TRS
/photo.jpg
/ hello1
/ hello1 /新

我有一个很难通过操纵字符串创建分层(下文提到的类)的结构出来,任何人都可以提出一个方法/ 。想法,我可以实现它。

 公共类DeltaItem 
{

私人列表< D​​eltaItem> ; _items;
公共字符串路径{搞定;组; }
公共BOOL ISDIR {搞定;组; }

公开名单< D​​eltaItem>项目
{
得到
{
返回_items? (_items =新的List< D​​eltaItem>());
}
}
}


解决方案

这是一个非常简单的解析操作。首先,我会定义类,像这样:

 公共类节点
{
私人只读IDictionary的< ;字符串,节点GT; _nodes =
新字典<字符串,节点>();

公共字符串路径{搞定;组; }
}



从那里它的问题:




  1. 解析路径(使用 \ 作为分隔符)。

  2. 向下遍历树,如果有必要增加新的节点。



您可以包装上面的一个方法添加

 公共无效让addpath(字符串路径)
{
的char [ ] charSeparators =新的char [] {'\\'};

//解析成份的序列。
的String [] =零件path.Split(charSeparators,
StringSplitOptions.RemoveEmptyEntries);

//当前节点。启动与此有关。
节点电流=这一点;

//迭代通过的部分。
的foreach(在部分字符串的一部分)
{
//子节点。
节点的孩子;

//是否参与当前节点存在吗?如果
//不行,再加入。
如果(!current._nodes.TryGetValue(部分,出子))
{
//添加的孩子。
=子新的Node {
路径=一部分
};

//添加到字典中。
current._nodes [一部分] =小孩;
}

//将当前给孩子。
电流=子女;
}



}



这会给你你需要的层次结构。您可以公开的字典,让您穿越它的工作业务,但是这是你要如何填充你要使用的一般结构。



请注意,你会开始与没有路径,然后通过你的名单上面的迭代,并调用让addpath 在上面的列表中的每个项目。


I'm playing with the Dropbox's Delta API, when I call delta method I get a list of path's that changed since last call.

/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 

I had a hard time creating hierarchical (in below mentioned class) structure out of it by manipulating the string, could anyone suggest a way/idea I can achieve it.

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>());
        }
    }
}

解决方案

It's a pretty simple parse operation. First, I'd define the class like so:

public class Node
{
    private readonly IDictionary<string, Node> _nodes = 
        new Dictionary<string, Node>();

    public string Path { get; set; }
}

From there it's a matter of:

  1. Parsing the path (using \ as the delimiter).
  2. Traversing down the tree, adding new nodes if necessary.

You can wrap the above in a single method Add:

public void AddPath(string path)
{
   char[] charSeparators = new char[] {'\\'};

   // Parse into a sequence of parts.
   string[] parts = path.Split(charSeparators, 
       StringSplitOptions.RemoveEmptyEntries);

   // The current node.  Start with this.
   Node current = this;

   // Iterate through the parts.
   foreach (string part in parts)
   {
       // The child node.
       Node child;

       // Does the part exist in the current node?  If
       // not, then add.
       if (!current._nodes.TryGetValue(part, out child))
       {
           // Add the child.
           child = new Node {
               Path = part
           };

           // Add to the dictionary.
           current._nodes[part] = child;
       }

       // Set the current to the child.
       current = child;
   }

}

This will give you the hierarchy you need. You can expose operations that work on the dictionary which will allow you to traverse it, but this is how you'd populate the general structure you'd be working with.

Note that you'd start off with a singular node that has no Path and then iterate through your list above and call AddPath on every item in the list above.

这篇关于如何创建带路径的目录层次结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆