Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.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从字符串路径构建文件夹/文件树#_C#_Algorithm_Treeview - Fatal编程技术网

C# 使用c从字符串路径构建文件夹/文件树#

C# 使用c从字符串路径构建文件夹/文件树#,c#,algorithm,treeview,C#,Algorithm,Treeview,我需要解决一个问题,涉及到从stirng路径构建树,以下是模型: public class Folder { public string Name { get; set; } public List<Folder> Folders { get; set; } = new List<Folder>(); public List<File> Files { get; set; } = new List<File>();

我需要解决一个问题,涉及到从stirng路径构建树,以下是模型:

public class Folder
{
    public string Name { get; set; }
    
    public List<Folder> Folders { get; set; } = new List<Folder>();
    public List<File> Files { get; set; } = new List<File>();
}

public class File 
{
    public string Name { get; set; }
}

非常感谢您的帮助:)

这里是您的代码:

using System;
using System.Collections.Generic;

namespace FolderTree
{
    public class Folder
    {
        public string Name { get; set; }
        public List<Folder> Folders { get; set; } = new List<Folder>();
        public List<File> Files { get; set; } = new List<File>();
    }

    public class File
    {
        public string Name { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var cars = new List<string>()
            {
                "Car/",
                "Car/BMW/",
                "Car/Great Wall/",
                "Car/Great Wall/Another/",
                "Car/Great Wall/Another/test - file.bak",
                "Car/Great Wall/Another/second - file.bak",
                "Car/Great Wall/Car/",
                "Car/Great Wall/local - copy.bak",
                "Car/Great Wall/local - copy(2).bak",
                "Car/Mercedes/process - file.bak",
                "Car/Mercedes/process - file(2).bak",
                "Car/test123 - file.bak"
            };

            var folders = GetFoldersFormStrings(cars);
            ShowFolders(folders);
        }

        static List<Folder> GetFoldersFormStrings(List<string> strings)
        {
            var folders = new List<Folder>();
            strings.Sort(StringComparer.InvariantCultureIgnoreCase);
            var folderByPath = new Dictionary<string, Folder>();
            foreach (var str in strings)
            {
                if (str.EndsWith("/")) // we have a folder
                {
                    EnsureFolder(folders, folderByPath, str);
                }
                else // we have a file
                {
                    var lastSlashPosition = str.LastIndexOf("/");
                    var parentFolderPath = str.Substring(0, lastSlashPosition + 1);
                    var parentFolder = EnsureFolder(folders, folderByPath, parentFolderPath);
                    var fileName = str.Substring(lastSlashPosition + 1);
                    var file = new File
                    {
                        Name = fileName
                    };
                    parentFolder.Files.Add(file);
                }
            }
            return folders;
        }

        private static Folder EnsureFolder(List<Folder> rootFolders, Dictionary<string, Folder> folderByPath, string folderPath)
        {
            if (!folderByPath.TryGetValue(folderPath, out var folder))
            {
                var folderPathWithoutEndSlash = folderPath.TrimEnd('/');
                var lastSlashPosition = folderPathWithoutEndSlash.LastIndexOf("/");
                List<Folder> folders;
                string folderName;
                if (lastSlashPosition < 0) // it's a first level folder
                {
                    folderName = folderPathWithoutEndSlash;
                    folders = rootFolders;
                }
                else
                {
                    var parentFolderPath = folderPath.Substring(0, lastSlashPosition + 1);
                    folders = folderByPath[parentFolderPath].Folders;
                    folderName = folderPathWithoutEndSlash.Substring(lastSlashPosition + 1);
                }
                folder = new Folder
                {
                    Name = folderName
                };
                folders.Add(folder);
                folderByPath.Add(folderPath, folder);
            }
            return folder;
        }

        private static void ShowFolders(List<Folder> folders)
        {
            foreach (var folder in folders)
            {
                ShowFolder(folder, 0);
            }
        }

        private static void ShowFolder(Folder folder, int indentation)
        {
            string folderIndentation = new string(' ', indentation);
            string fileIndentation = folderIndentation + "  ";
            Console.WriteLine($"{folderIndentation}-{folder.Name}");
            foreach (var file in folder.Files)
            {
                Console.WriteLine($"{fileIndentation}-{file.Name}");
            }
            foreach (var subfolder in folder.Folders)
            {
                ShowFolder(subfolder, indentation + 2);
            }
        }
    }
}

如何判断字符串是文件还是文件夹?文件夹名称也可以有扩展名。不,在我的情况下,文件夹没有扩展名,只有文件有扩展名。此外,数据直接来自数据库还有无扩展名的文件名。Car/BMW是一个文件还是一个文件夹?这是一个文件夹,我的坏消息,它必须在末尾有“/”这正是我要找的,问题标记为重复,但引用的重复答案不是所需的。这是必要的答案,谢谢!:D
-Car
  -BMW
  -Great Wall
    -Another
      -test - file.bak
      -second - file.bak
    -Car
  -local - copy.bak
  -local - copy(2).bak
  -Mercedes
    -process - file.bak
    -process - file(2).bak
  -test123 - file.bak
using System;
using System.Collections.Generic;

namespace FolderTree
{
    public class Folder
    {
        public string Name { get; set; }
        public List<Folder> Folders { get; set; } = new List<Folder>();
        public List<File> Files { get; set; } = new List<File>();
    }

    public class File
    {
        public string Name { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var cars = new List<string>()
            {
                "Car/",
                "Car/BMW/",
                "Car/Great Wall/",
                "Car/Great Wall/Another/",
                "Car/Great Wall/Another/test - file.bak",
                "Car/Great Wall/Another/second - file.bak",
                "Car/Great Wall/Car/",
                "Car/Great Wall/local - copy.bak",
                "Car/Great Wall/local - copy(2).bak",
                "Car/Mercedes/process - file.bak",
                "Car/Mercedes/process - file(2).bak",
                "Car/test123 - file.bak"
            };

            var folders = GetFoldersFormStrings(cars);
            ShowFolders(folders);
        }

        static List<Folder> GetFoldersFormStrings(List<string> strings)
        {
            var folders = new List<Folder>();
            strings.Sort(StringComparer.InvariantCultureIgnoreCase);
            var folderByPath = new Dictionary<string, Folder>();
            foreach (var str in strings)
            {
                if (str.EndsWith("/")) // we have a folder
                {
                    EnsureFolder(folders, folderByPath, str);
                }
                else // we have a file
                {
                    var lastSlashPosition = str.LastIndexOf("/");
                    var parentFolderPath = str.Substring(0, lastSlashPosition + 1);
                    var parentFolder = EnsureFolder(folders, folderByPath, parentFolderPath);
                    var fileName = str.Substring(lastSlashPosition + 1);
                    var file = new File
                    {
                        Name = fileName
                    };
                    parentFolder.Files.Add(file);
                }
            }
            return folders;
        }

        private static Folder EnsureFolder(List<Folder> rootFolders, Dictionary<string, Folder> folderByPath, string folderPath)
        {
            if (!folderByPath.TryGetValue(folderPath, out var folder))
            {
                var folderPathWithoutEndSlash = folderPath.TrimEnd('/');
                var lastSlashPosition = folderPathWithoutEndSlash.LastIndexOf("/");
                List<Folder> folders;
                string folderName;
                if (lastSlashPosition < 0) // it's a first level folder
                {
                    folderName = folderPathWithoutEndSlash;
                    folders = rootFolders;
                }
                else
                {
                    var parentFolderPath = folderPath.Substring(0, lastSlashPosition + 1);
                    folders = folderByPath[parentFolderPath].Folders;
                    folderName = folderPathWithoutEndSlash.Substring(lastSlashPosition + 1);
                }
                folder = new Folder
                {
                    Name = folderName
                };
                folders.Add(folder);
                folderByPath.Add(folderPath, folder);
            }
            return folder;
        }

        private static void ShowFolders(List<Folder> folders)
        {
            foreach (var folder in folders)
            {
                ShowFolder(folder, 0);
            }
        }

        private static void ShowFolder(Folder folder, int indentation)
        {
            string folderIndentation = new string(' ', indentation);
            string fileIndentation = folderIndentation + "  ";
            Console.WriteLine($"{folderIndentation}-{folder.Name}");
            foreach (var file in folder.Files)
            {
                Console.WriteLine($"{fileIndentation}-{file.Name}");
            }
            foreach (var subfolder in folder.Folders)
            {
                ShowFolder(subfolder, indentation + 2);
            }
        }
    }
}
-Car
  -test123 - file.bak
  -BMW
  -Great Wall
    -local - copy(2).bak
    -local - copy.bak
    -Another
      -second - file.bak
      -test - file.bak
    -Car
  -Mercedes
    -process - file(2).bak
    -process - file.bak