Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void Main()
- {
- string[] paths = new[] { "/a/b", "/a/bb", "/a/bbb", "/b/a", "/b/aa", "/b/aaa", "/c/d", "/d/e" };
- string root = "/";
- Console.WriteLine(string.Join(", ", paths.Select(s => GetSubdirectory(root, s)).Where(s => s != null).Distinct()));
- }
- static string GetSubdirectory(string root, string path)
- {
- string subDirectory = null;
- int index = path.IndexOf(root);
- Console.WriteLine(index);
- if (root != path && index == 0)
- {
- subDirectory = path.Substring(root.Length, path.Length - root.Length).Trim('/').Split('/')[0];
- }
- return subDirectory;
- }
- var startingPath = @"c:";
- var directoryInfo = new DirectoryInfo(startingPath);
- var result = directoryInfo.GetDirectories().Select(x => x.FullName).ToArray();
- void Main()
- {
- string [] paths = { @"/a/b", @"/a/bb", @"/a/bbb", @"/b/a", @"/b/aa", @"/b/aaa", @"/c/d", @"/d/e" };
- var result = paths.Select(x => x.Split('/')[1]).Distinct();
- result.Dump();
- }
- var result = paths.Select(x =>x.Split(new string [] {"/"},
- StringSplitOptions.RemoveEmptyEntries)[0])
- .Distinct();
- var rootList = new List <string>();
- foreach (var fullPath in myPaths)
- {
- rootList.Add(Path.GetPathRoot(fullPath))
- }
- return rootList.Distinct();
- myPaths.Select(x => Path.GetPathRoot(x)).Distinct();
- myPaths.Select(x => Directory.GetDirectoryRoot(x)).Distinct();
- string dir = @"C:Level1Level2;
- string root = Path.GetPathRoot(dir);
- string pathWithoutRoot = dir.Substring(root.Length);
- string firstDir = pathWithoutRoot.Split(Path.DirectorySeparatorChar).First();
- string currentDirectory = "/";
- var distinctDirectories = paths.Where(p => p.StartsWith(currentDirectory)
- .Select(p => GetFirstSubDir(p, currentDirectory)).Distinct();
- ...
- string GetFirstSubDir(string path, string currentDirectory)
- {
- int index = path.IndexOf('/', currentDirectory.Length);
- if (index >= 0)
- return path.SubString(currentDirectory.Length - 1, index + 1 - currentDirectory.Length);
- return path.SubString(currentDirectory.Length - 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement