1

我正在尝试从 DOS 树命令 (tree /F /A > treeList.txt) 的输出中填充 C# TreeView。我需要逐行确定文本文件中每个节点的级别并将其存储为整数。有没有办法可以通过正则表达式来确定?下面是 Tree 命令的输出示例:

Folder PATH listing
Volume serial number is ****-****
C:.
|   info.txt
|   treeList.txt
|   
+---Folder1
|   +---Sub1
|   |   |   info.txt
|   |   |   info2.txt
|   |   |   info3.txt
|   |   |   
|   |   \---Sub
|   |       |   info.txt
|   |       |   
|   |       \---Sub
|   |               info.txt
|   |               info2.txt
|   |               
|   +---Sub2
|   \---Sub3
+---Folder2
|   |   info.txt
|   |   info2.txt
|   |   
|   +---Sub1
|   |       info.txt
|   |       
|   +---Sub2
|   +---Sub3
|   |       info.txt
|   |       
|   \---Sub4
+---Folder3
|   \---Sub1
+---Folder4
|   +---Sub1
|   \---Sub2
|           info.txt
|           info2.txt
|           
\---Folder5
        info.txt

这是我试图实现的输出示例:

info.txt     0
treeList.txt 0
Folder1      0
Sub1         1
info.txt     2
info2.txt    2
info3.txt    2
Sub          2
info.txt     3
Sub          3
info.txt     4
info2.txt    4
Folder2      0
And so on...

非常感谢任何帮助或指导。

4

2 回答 2

1

我有个主意。您可能希望通过替换来替换树字符串的每一行中的每个特殊字符:

[|\\-\+]

然后计算行首和文件或文件夹名称之间的空格。空格数会告诉你你在 lvl 中有多深。然后您也可以将空格数除以 3,您将获得大约 lvl 数。你怎么看?

于 2013-11-12T17:56:35.943 回答
1

用于在节点开头分割文本的表达式:

((?:[a-zA-Z0-9][+'-_ ()a-zA-Z0-9.]*))

用于确定节点级别的代码:

    List<TreeItem> items=new List<TreeItem>();

    int lineNum=0;
    string line;

    // Read the file
    StreamReader file=new StreamReader("<Path>");

    while((line=file.ReadLine())!=null) {
        string[] parts=Regex.Split(line,"((?:[a-zA-Z0-9][+'-_ ()a-zA-Z0-9.]*))");
        if(parts.Length>1) {
            //Node level is determined by the number of characters preceding node text
            items.Add(new TreeItem(parts[1],(parts[0].Length/4)-1));
        }
        lineNum++;
    }

    file.Close();
于 2013-11-19T19:11:16.583 回答