Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从分组文件路径集合中提取公共路径?_C#_String_Pattern Matching_Directory - Fatal编程技术网

C# 从分组文件路径集合中提取公共路径?

C# 从分组文件路径集合中提取公共路径?,c#,string,pattern-matching,directory,C#,String,Pattern Matching,Directory,我有一个关于的问题,我的源代码集合包含一个文件路径列表,这些路径并不总是共享一个公共路径(有时在C:\drive之外),例如: 资料来源: C:\Test\Root\Common\Data\a.txt C:\Test\Root\Common\Data\Home\b.txt C:\Test\Root\Common\Data\Home\Dev\c.txt C:\Test2\Random\Data\a.txt C:\Test2\Random\b.txt C:\Test2\c.txt D:\Data\a

我有一个关于的问题,我的源代码集合包含一个文件路径列表,这些路径并不总是共享一个公共路径(有时在C:\drive之外),例如:

资料来源:

C:\Test\Root\Common\Data\a.txt
C:\Test\Root\Common\Data\Home\b.txt
C:\Test\Root\Common\Data\Home\Dev\c.txt
C:\Test2\Random\Data\a.txt
C:\Test2\Random\b.txt
C:\Test2\c.txt
D:\Data\a.txt
C:\Test\Root\Common\Data\
C:\Test2\
D:\Data\
输出应该是一个集合:

C:\Test\Root\Common\Data\a.txt
C:\Test\Root\Common\Data\Home\b.txt
C:\Test\Root\Common\Data\Home\Dev\c.txt
C:\Test2\Random\Data\a.txt
C:\Test2\Random\b.txt
C:\Test2\c.txt
D:\Data\a.txt
C:\Test\Root\Common\Data\
C:\Test2\
D:\Data\

如何找到每个文件路径“组”的公共路径?我在这里找到了许多解决方案,但总是有一组文件路径共享至少一个公共目录,而这里的情况并非如此。

我仍然不确定我是否正确理解了这个问题

我希望这能奏效

    public List<string> ExtractCommonPaths(List<string> paths)
    {
        var separatedImput = paths
            .Select(path => path.Split(new [] {":\\", "\\" }, StringSplitOptions.RemoveEmptyEntries))
            .Select(path => path.Take(path.Length - 1).ToList());
        return separatedImput.GroupBy(path => path[0] + ":\\" + path[1])
            .Select(g =>
            {
                var commonPath = g.Key;
                var commpoPathLength = 2;
                for (;;)
                {
                    var exit = false;
                    var pathItem = string.Empty;
                    foreach (var path in g)
                    {
                        if (path.Count <= commpoPathLength)
                        {
                            exit = true;
                            break;
                        }

                        if (pathItem == string.Empty)
                            pathItem = path[commpoPathLength];
                        else
                        {
                            if (pathItem != path[commpoPathLength])
                            {
                                exit = true;
                                break;
                            }
                        }
                    }

                    if (exit)
                        break;
                    commonPath += "\\" + pathItem;
                    commpoPathLength++;
                }

                return commonPath;
            })
            .ToList();
    }
公共列表路径(列表路径)
{
var separatedImput=路径
.Select(path=>path.Split(新[]{:\\”,“\\”},StringSplitOptions.RemoveEmptyEntries))
.Select(path=>path.Take(path.Length-1.ToList());
返回separatedImput.GroupBy(路径=>path[0]+“:\\\”+路径[1])
.选择(g=>
{
var commonPath=g.键;
var commpoPathLength=2;
对于(;;)
{
var exit=false;
var pathItem=string.Empty;
foreach(g中的变量路径)
{

如果(path.Count如果您想查找某个位置中的目录,我有一些东西。 对于这个方法,我有一些(C:\Files1)示例位置的每个目录

如果只想从此列表中获取主目录:

public List<DirectoryInfo> ExtractDirectoriesCommonPaths(List<DirectoryInfo> directories, string location)
    {
        var result = new List<DirectoryInfo>() { };
        directories.ForEach(directory =>
        {
            var otherDirectories = directories.Where(d => d.FullName != directory.FullName);
            var anyDirectoryWithThisSamePath = otherDirectories.Where(x => directory.FullName.Contains(x.FullName) && x.FullName.Length < directory.FullName.Length);
            if (anyDirectoryWithThisSamePath.Any())
            {
                result.Add(anyDirectoryWithThisSamePath.FirstOrDefault());
            }
        });

        return result.Where(x => x.FullName != location && x.FullName.Length > location.Length).Distinct().ToList();
    }
输出:

C:\Files1\test_announcements
C:\Files1\Parent

为什么
“C:\Test\Root\Common\Data\Home\”
不能成为输出的一部分?为什么C:\和D:\?@maccettura不能成为输出的一部分,因为前三个路径的公共文件夹是C:\Test\Root\Common\Data@Valerii我需要按最低公用文件夹而不是驱动器号对它们进行分组:)@Coloris如果要按“最低公用文件夹”进行分组,则
“C:\Test\Root\Common\Data\Home\”
应该是一个输出。您的答案一点也不清楚。谢谢@valeri,这几乎是完美的,只是如果只有一个文件路径实例,它会返回完整的文件名D:\Data\a.txt,而不是D:\Data。我已经修复了我的解决方案。谢谢,我要深入研究您的代码以理解逻辑:)如果路径仅包含1个“段”,例如
C:\1.txt
C:\Temp
@Cocowalla Yes,则会引发
GroupBy
行。看起来应该添加额外的检查。