C# C如何使用Directory.GetPath LINQ限制子文件夹结果

C# C如何使用Directory.GetPath LINQ限制子文件夹结果,c#,winforms,C#,Winforms,我有这个功能: var dupeFiles = Directory.EnumerateFiles(strPath, "*", SearchOption.AllDirectories) .Select(fullPath => new { Name = Path.GetFileName(fullPath), FullPath = fullPath })

我有这个功能:

var dupeFiles = Directory.EnumerateFiles(strPath, "*", SearchOption.AllDirectories)
            .Select(fullPath => new
            {
                Name = Path.GetFileName(fullPath),
                FullPath = fullPath
            })
            .GroupBy(file => file.Name)
            .Where(fileGroup => fileGroup.Count() > 1);


        foreach (var dupeGroup in dupeFiles)
        {
            using (System.IO.StreamWriter file =
                new System.IO.StreamWriter(@"DupeFiles.txt", true))
            {
                file.WriteLine(dupeGroup.Key);
            }

            foreach (var dupeFile in dupeGroup)
            {
                using (System.IO.StreamWriter file =
        new System.IO.StreamWriter(@"DupeFiles.txt", true))
                {
                    file.WriteLine($"   {dupeFile.FullPath}");
                }

            }
        }
但我得到的结果是:

FILE.TXT
     ...\RootFolder\Folder1\Child1\AnotherChild\FILE.TXT
     ...\RootFolder\Folder1\Child2\AnotherChild\FILE.TXT
     ...\RootFolder\Folder1\Child3\FILE.TXT
     ...\RootFolder\Folder2\Child3\FILE.TXT
     ...\RootFolder\Folder2\Child2\AnotherChild\FILE.TXT
如何限制来自folder1和folder2或更多的相同文件路径的结果,即相同的子级?在这种情况下,我只想要结果:

FILE.TXT
   ...\RootFolder\Folder1\Child2\AnotherChild\FILE.TXT
   ...\RootFolder\Folder2\Child2\AnotherChild\FILE.TXT
谢谢大家!

谢谢大家的回复,但还是没用。也许我不能告诉你我到底需要什么,所以看看那些真实的照片:

如您所见,第一个文件:NMS_REALITY_GCTECHNOLOGYTABLE.MBIN在3个文件夹中重复:

莱索弗赫特10。。。 降低了发射成本。。。 _贝特货船

没关系,因为正如您所看到的,同一个文件位于不同的根文件夹中

但另一个文件PLANTINTERACTION.ENTITY.MBIN在许多其他文件夹中重复,但在同一文件夹中:

_更快的农业

这部分是错误的。我想忽略不列出所有这些文件,因为它是为同一个文件夹。我只想列出,或标记为冲突,如果它在相同的路径,但不同的根文件夹

现在好多了?非常感谢你,如果不能正确解释我的需求,我很抱歉


相信你,谢谢

创建一个ParentPath属性并按其分组,然后通过跳过所有不需要的内容从每个parentDirectory中选择所需内容

    var dupeFiles = Directory.EnumerateFiles(strPath, "*", SearchOption.AllDirectories)
    .Select(fullPath => new
    {
        Name = Path.GetFileName(fullPath),
        FullPath = fullPath,
        ParentPath = GetParentPath(fullPath)
        }).GroupBy(file => file.ParentPath)
        .Select(parentDirectory => parentDirectory
            .SkipWhile((childFile, index) => index != 1)
            .First()
         );


        StreamWriter fileWriter = new StreamWriter(@"DupeFiles.txt", true);
        foreach(var aFile in dupeFiles.ToList())
           Console.WriteLine(aFile.FullPath);

使用双foreach和保留i分组键编辑答案
谢谢你的帮助!我测试了代码,但它不起作用=foreach aFile始终只返回1个选项。我更改了.SkipWhile中的索引,返回的文件不同,但它只返回一个文件=尝试了foreach的代码部分,但没有标识.key参数和dupeGroup=任何提示,请?泰!!!!代码对我有用。strPath应该是RootFolder的路径,例如字符串strPath=C:\\Users\\Luke\\Desktop\\RootFolder;无关的提示:考虑将流写器放置在Frach循环外,以避免每次写入行类型时打开/查找/关闭。
    private string GetParentPath(string fullPath)
    {
        string [] strArr = fullPath.Split('\\');
        string parentPath = strArr[0] + '\\' + strArr[1] + '\\' + strArr[2] + '\\' + strArr[3] + '\\' + strArr[4] + '\\' + strArr[5];
        //Console.WriteLine("parentPath: " + parentPath);
        return parentPath;
    }
        string strPath = "C:\\Users\\Luke\\Desktop\\RootFolder";
        var dupeFiles = Directory.EnumerateFiles(strPath, "*", SearchOption.AllDirectories)
        .Select(fullPath => new
        {
            Name = Path.GetFileName(fullPath),
            FullPath = fullPath,
        }).GroupBy(file => GetParentPath(file.FullPath))
            .Select(parentDirectory => 
            parentDirectory.SkipWhile((childFile, index) => index != 1).First()
             ).GroupBy(file => file.Name, file => file.FullPath);



        foreach(var dupeGroup in dupeFiles)
        {
            Console.WriteLine("key is: " + dupeGroup.Key);
            foreach (var dupeFile in dupeGroup)
            {
                Console.WriteLine(dupeFile);
            }
        }

        /*example output
        
        key is: FILE.txt
        C:\Users\Luke\Desktop\RootFolder\Folder1\Child2\AnotherChild\FILE.txt
        C:\Users\Luke\Desktop\RootFolder\Folder2\Child2\AnotherChild\FILE.txt
        
        */