У меня есть строковый массив путей к некоторым файлам:
path/to/folder/file.xxx
path/to/other/
path/to/file/file.xx
path/file.x
path/
Как я могу преобразовать этот список в древовидную структуру? Пока что у меня есть следующее:
/// <summary>
/// Enumerates types of filesystem nodes.
/// </summary>
public enum FilesystemNodeType
{
/// <summary>
/// Indicates that the node is a file.
/// </summary>
File,
/// <summary>
/// Indicates that the node is a folder.
/// </summary>
Folder
}
/// <summary>
/// Represents a file or folder node.
/// </summary>
public class FilesystemNode
{
private readonly ICollection<FilesystemNode> _children;
/// <summary>
/// Initializes a new instance of the <see cref="FilesystemNode"/> class.
/// </summary>
public FilesystemNode()
{
_children = new LinkedList<FilesystemNode>();
}
/// <summary>
/// Gets or sets the name of the file or folder.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets the full path to the file or folder from the root.
/// </summary>
public string Path { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the node is a file or folder.
/// </summary>
public FilesystemNodeType Type { get; set; }
/// <summary>
/// Gets a list of child nodes of this node. The node type must be a folder to have children.
/// </summary>
public ICollection<FilesystemNode> Children
{
get
{
if (Type == FilesystemNodeType.Folder)
return _children;
throw new InvalidOperationException("File nodes cannot have children");
}
}
}
Я просто немного не понимаю, как на самом деле разделить пути и все остальное. Любой путь, который заканчивается на /, является каталогом, любой, который не заканчивается, - нет.
Кроме того, хотя мой вход всегда будет содержать путь к папке, как бы я учитывал ситуацию, если бы его не было?
Например, если бы у меня был вход:
path/to/file.c
path/file.c
path/
Как бы я учитывал тот факт, что path/to/
не находится во входе?