Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 12th, 2012  |  syntax: None  |  size: 2.93 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How can I convert a list of filenames to a tree structure?
  2. path/to/folder/file.xxx
  3. path/to/other/
  4. path/to/file/file.xx
  5. path/file.x
  6. path/
  7.        
  8. /// <summary>
  9. /// Enumerates types of filesystem nodes.
  10. /// </summary>
  11. public enum FilesystemNodeType
  12. {
  13.     /// <summary>
  14.     /// Indicates that the node is a file.
  15.     /// </summary>
  16.     File,
  17.  
  18.     /// <summary>
  19.     /// Indicates that the node is a folder.
  20.     /// </summary>
  21.     Folder
  22. }
  23.  
  24. /// <summary>
  25. /// Represents a file or folder node.
  26. /// </summary>
  27. public class FilesystemNode
  28. {
  29.     private readonly ICollection<FilesystemNode> _children;
  30.  
  31.     /// <summary>
  32.     /// Initializes a new instance of the <see cref="FilesystemNode"/> class.
  33.     /// </summary>
  34.     public FilesystemNode()
  35.     {
  36.         _children = new LinkedList<FilesystemNode>();
  37.     }
  38.  
  39.     /// <summary>
  40.     /// Gets or sets the name of the file or folder.
  41.     /// </summary>
  42.     public string Name { get; set; }
  43.  
  44.     /// <summary>
  45.     /// Gets or sets the full path to the file or folder from the root.
  46.     /// </summary>
  47.     public string Path { get; set; }
  48.  
  49.     /// <summary>
  50.     /// Gets or sets a value indicating whether the node is a file or folder.
  51.     /// </summary>
  52.     public FilesystemNodeType Type { get; set; }
  53.  
  54.     /// <summary>
  55.     /// Gets a list of child nodes of this node. The node type must be a folder to have children.
  56.     /// </summary>
  57.     public ICollection<FilesystemNode> Children
  58.     {
  59.         get
  60.         {
  61.             if (Type == FilesystemNodeType.Folder)
  62.                 return _children;
  63.  
  64.             throw new InvalidOperationException("File nodes cannot have children");
  65.         }
  66.     }
  67. }
  68.        
  69. path/to/file.c
  70. path/file.c
  71. path/
  72.        
  73. public class NodeEntry
  74. {
  75.     public NodeEntry()
  76.     {
  77.         this.Children = new NodeEntryCollection();
  78.     }
  79.  
  80.     public string Key { get; set; }
  81.     public NodeEntryCollection Children { get; set; }
  82.  
  83. }
  84.  
  85. public class NodeEntryCollection : Dictionary<string, NodeEntry>
  86. {
  87.     public void AddEntry(string sEntry, int wBegIndex)
  88.     {
  89.         if (wBegIndex < sEntry.Length)
  90.         {
  91.             string sKey;
  92.             int wEndIndex;
  93.  
  94.             wEndIndex = sEntry.IndexOf("/", wBegIndex);
  95.             if (wEndIndex == -1)
  96.             {
  97.                 wEndIndex = sEntry.Length;
  98.             }
  99.             sKey = sEntry.Substring(wBegIndex, wEndIndex - wBegIndex);
  100.             if (!string.IsNullOrEmpty(sKey)) {
  101.                 NodeEntry oItem;
  102.  
  103.                 if (this.ContainsKey(sKey)) {
  104.                     oItem = this[sKey];
  105.                 } else {
  106.                     oItem = new NodeEntry();
  107.                     oItem.Key = sKey;
  108.                     this.Add(sKey, oItem);
  109.                 }
  110.                 // Now add the rest to the new item's children
  111.                 oItem.Children.AddEntry(sEntry, wEndIndex + 1);
  112.             }
  113.         }
  114.     }
  115. }
  116.        
  117. NodeEntryCollection cItems = new NodeEntryCollection();
  118.        
  119. cItems.AddEntry(sLine, 0);
  120.        
  121. string.IsNullOrEmpty(new FileInfo("test").Extension)