C# { 公共字符串名称{get;private set;} 公共节点集子节点{get;private set;} 公共节点(字符串名称) { if(String.IsNullOrWhiteSpace(name))抛出新的ArgumentNullExceptio

C# { 公共字符串名称{get;private set;} 公共节点集子节点{get;private set;} 公共节点(字符串名称) { if(String.IsNullOrWhiteSpace(name))抛出新的ArgumentNullExceptio,c#,list,collections,recursion,directory,C#,List,Collections,Recursion,Directory,{ 公共字符串名称{get;private set;} 公共节点集子节点{get;private set;} 公共节点(字符串名称) { if(String.IsNullOrWhiteSpace(name))抛出新的ArgumentNullException(“name”); 名称=名称; Children=新节点集(); } 公共重写字符串ToString(){return ToString(1);} 私有字符串到字符串(Int32缩进) { var indentStr=Environment

{ 公共字符串名称{get;private set;} 公共节点集子节点{get;private set;} 公共节点(字符串名称) { if(String.IsNullOrWhiteSpace(name))抛出新的ArgumentNullException(“name”); 名称=名称; Children=新节点集(); } 公共重写字符串ToString(){return ToString(1);} 私有字符串到字符串(Int32缩进) { var indentStr=Environment.NewLine+新字符串('\t',indent); 返回Name+(Children.Count==0?“:indentStr+String.Join(indentStr,Children.Select(c=>c.ToString(indent+1)).ToArray()); } } 公共类节点集:KeyedCollection{ 受保护的重写字符串GetKeyForItem(节点项){return item.Name;} } } }
这是家庭作业吗?如果是这样的话,就应该贴上这样的标签。这绝对不是家庭作业。这是供我自己使用的,因为我正在通过使用一个以所示格式列出这些路径的文件,以编程方式为WiX创建一个.wxs文件。但是这真的很重要吗。。。这只是一个有趣的小东西,他写了很多代码:)我建议你给自己一个真实的名字,和一些简历,如果你想被认真对待的话。为了简洁起见,我做了一些让步,比如公共财产。在现实生活中,不要这样做!一些额外的验证也很好。Andrew这正是我想要的,只是我不需要打印出来,对象才是最重要的。我知道我做错了,但我还是发布了我的代码,这样你就可以看到我尝试了什么。谢谢你的时间和努力,我能看到我现在偏离了方向,我从你的努力中学到了东西。谢谢!:))没问题。打印出来很容易,而且有助于调试。在手表窗口里翻来翻去验证这些东西一点都不好玩。另外,我不知道你是否需要“家长”推荐信。。。如果是这样,只需将其更改为具有
新节点{Parent=this,Name=…}
,并添加一个Parent{}属性。谢谢Chris,这很有帮助。谢谢你抽出时间。首先,节点值不起作用,因为它基本上包含整个字符串,我需要第一个节点只是ProgramDir,然后下一个节点是InstallDir,依此类推。我不需要打印出来,重要的是对象。再次,非常有帮助,我衷心感谢您抽出时间。谢谢。:)不确定您的意思-我的代码构建了一个节点对象树,其中根对象是唯一的根目录集,每个根目录包含该根的唯一子目录集,依此类推。我想这就是你想要的。
        List<string> filePaths = new List<string>();
        filePaths.Add(@"ProgramDir\InstallDir\Module1\mod1pack1.exe");
        filePaths.Add(@"ProgramDir\InstallDir\Module1\mod1pack2.exe");
        filePaths.Add(@"ProgramDir\InstallDir\Module2\mod2pack1.exe");
        filePaths.Add(@"ProgramDir\InstallDir\Module1\SubModule1\report1.rpt");
        filePaths.Add(@"SystemDir\DependencyDir\dependency1.dll");
        filePaths.Add(@"SystemDir\DependencyDir\dependency2.dll");
-ProgramDir
    Installdir
        Module1
            mod1pack1.exe
            mod1pack2.exe
            -SubModule1
                 report1.rpt
        Module2
            mod2pack1.exe
-SystemDir
    -DependencyDir
         dependency1.dll
     dependency2.dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SetFilePathList();
            DTree forest = new DTree();
            List<DTreeBranch> branches = new List<DTreeBranch>();
            foreach (string path in filePaths)
            {
                forest.GrowTree(path.Split('\\'), branches);
            }
            forest.SubBranches.AddRange(branches);
        }
        private static List<string> filePaths { get; set; }
        private static void SetFilePathList()
        {
            filePaths = new List<string>();
            filePaths.Add(@"ProgramDir\InstallDir\Module1\mod1pack1.exe");
            filePaths.Add(@"ProgramDir\InstallDir\Module1\mod1pack2.exe");
            filePaths.Add(@"ProgramDir\InstallDir\Module2\mod2pack1.exe");
            filePaths.Add(@"ProgramDir\InstallDir\Module1\SubModule1\report1.rpt");
            filePaths.Add(@"SystemDir\DependencyDir\dependency1.dll");
            filePaths.Add(@"SystemDir\DependencyDir\dependency2.dll");
        }
    }
    public class DTree
    {
        public List<DTreeBranch> SubBranches { get; set; }
        public string BranchName { get; set; }
        public DTree() { SubBranches = new List<DTreeBranch>(); }
        public DTreeBranch AddChildren(string[] childElements, DTreeBranch branch)
        {
            DTreeBranch childBranch;
            foreach (string element in childElements)
            {
                childBranch = new DTreeBranch();
                childBranch.BranchName = element;
                branch.SubBranches.Add(childBranch);
                var query = from q in childElements
                            where q != childBranch.BranchName
                            select q;
                AddChildren(query.ToArray<string>(), childBranch);
            }
            return branch;
        }
        public void GrowTree(string[] pathElements, List<DTreeBranch> Branches)
        {
            DTreeBranch result = Branches.Find(delegate(DTreeBranch b)
            {
                return b.BranchName == pathElements[0];
            });

            if (result == null)
            {
                DTreeBranch newRootBranch = new DTreeBranch();
                newRootBranch.BranchName = pathElements[0];
                Branches.Add(newRootBranch);
                GrowTree(pathElements, Branches);
            }
            else
            {
                var query = from q in pathElements
                            where q != result.BranchName
                            select q;
                DTreeBranch childBranch = AddChildren(query.ToArray<string>(), result);
                Branches.Add(childBranch);
            }
        }
    }
    public class DTreeBranch
    {
        public List<DTreeBranch> SubBranches { get; set; }
        public string BranchName { get; set; }
        public DTreeBranch()
        {
            SubBranches = new List<DTreeBranch>();
        }
    }
}
public class Node
{
    public string Name { get; set; }
    public bool IsDirectory { get; set; }
    public List<Node> Children = new List<Node>();

    internal void AddChildren(string f)
    {
        var dirs = Path.GetDirectoryName(f);
        if (string.IsNullOrEmpty(dirs))
        {
            // we are adding a file
            var file = Path.GetFileName(f);
            Children.Add(new Node {Name = file, IsDirectory = false});
        }
        else
        {
            // we are adding a directory
            var firstDir = dirs.Split(Path.DirectorySeparatorChar)[0];
            var childNode = Children.FirstOrDefault(d => d.Name == firstDir);
            if (childNode == null)
            {
                childNode = new Node {Name = firstDir, IsDirectory = true};
                Children.Add(childNode);
            }

            var subPath = f.Substring(firstDir.Length + 1);
            childNode.AddChildren(subPath);
        }
    }
}
var filePaths = new List<string> { 
    @"ProgramDir\InstallDir\Module1\mod1pack1.exe",
    @"ProgramDir\InstallDir\Module1\mod1pack2.exe",
    @"ProgramDir\InstallDir\Module2\mod2pack1.exe",
    @"ProgramDir\InstallDir\Module1\SubModule1\report1.rpt",
    @"SystemDir\DependencyDir\dependency1.dll",
    @"SystemDir\DependencyDir\dependency2.dll",
};

var node = new Node { Name = "Root", IsDirectory = true };
foreach (var f in filePaths )
{
    node.AddChildren(f);
}
public static void PrintNode(Node node, int indent)
{
    if (indent > 0) // don't print out root directory (level 1). 
    {
        var ending = node.IsDirectory ? Path.DirectorySeparatorChar.ToString() : "*";
        Console.WriteLine("{0}{1}{2}", new string('\t', indent - 1), node.Name, ending);
    }
    node.Children.ForEach(n => PrintNode(n, indent + 1));
}

ProgramDir\
    InstallDir\
            Module1\
                    mod1pack1.exe*
                    mod1pack2.exe*
                    SubModule1\
                            report1.rpt*
            Module2\
                    mod2pack1.exe*
SystemDir\
    DependencyDir\
            dependency1.dll*
            dependency2.dll*
namespace ConsoleApplication1
{
    class Program
    {
        public static void Main(String[] args)
        {
            var filePaths = new List<string> {@"ProgramDir\InstallDir\Module1\mod1pack1.exe", @"ProgramDir\InstallDir\Module1\mod1pack2.exe", @"ProgramDir\InstallDir\Module2\mod2pack1.exe", @"ProgramDir\InstallDir\Module1\SubModule1\report1.rpt", @"SystemDir\DependencyDir\dependency1.dll", @"SystemDir\DependencyDir\dependency2.dll"};
            var nodes = Parse(filePaths.ToArray());
            foreach (var node in nodes)
                Console.Out.WriteLine(node.ToString());
            Console.ReadLine();
        }

        public static IEnumerable<Node> Parse(params String[] paths)
        {
            var roots = new NodeSet();
            foreach (var path in paths)
            {
                var pathSplit = path.Split('\\');
                Node current = null;
                foreach (var pathElement in pathSplit)
                {
                    var currentRoots = (current == null) ? roots : current.Children;
                    if (currentRoots.Contains(pathElement))
                        current = currentRoots[pathElement];
                    else
                        currentRoots.Add(current = new Node(pathElement));
                }
            }
            return roots;
        }

        public class Node
        {
            public String Name { get; private set; }
            public NodeSet Children { get; private set; }

            public Node(String name)
            {
                if (String.IsNullOrWhiteSpace(name)) throw new ArgumentNullException("name");
                Name = name;
                Children = new NodeSet();
            }

            public override string ToString() { return ToString(1); }

            private String ToString(Int32 indent)
            {
                var indentStr = Environment.NewLine + new string('\t', indent);
                return Name + (Children.Count == 0 ? "" : indentStr + String.Join(indentStr, Children.Select(c => c.ToString(indent + 1)).ToArray()));
            }
        }

        public class NodeSet : KeyedCollection<String, Node> {
            protected override string GetKeyForItem(Node item) { return item.Name; }
        }
    }
}