Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. public static void Main()
  2. {
  3. string[] paths = new[] { "/a/b", "/a/bb", "/a/bbb", "/b/a", "/b/aa", "/b/aaa", "/c/d", "/d/e" };
  4. string root = "/";
  5.  
  6. Console.WriteLine(string.Join(", ", paths.Select(s => GetSubdirectory(root, s)).Where(s => s != null).Distinct()));
  7. }
  8.  
  9. static string GetSubdirectory(string root, string path)
  10. {
  11. string subDirectory = null;
  12. int index = path.IndexOf(root);
  13.  
  14. Console.WriteLine(index);
  15. if (root != path && index == 0)
  16. {
  17. subDirectory = path.Substring(root.Length, path.Length - root.Length).Trim('/').Split('/')[0];
  18. }
  19.  
  20. return subDirectory;
  21. }
  22.  
  23. var startingPath = @"c:";
  24.  
  25. var directoryInfo = new DirectoryInfo(startingPath);
  26.  
  27. var result = directoryInfo.GetDirectories().Select(x => x.FullName).ToArray();
  28.  
  29. void Main()
  30. {
  31. string [] paths = { @"/a/b", @"/a/bb", @"/a/bbb", @"/b/a", @"/b/aa", @"/b/aaa", @"/c/d", @"/d/e" };
  32.  
  33. var result = paths.Select(x => x.Split('/')[1]).Distinct();
  34.  
  35. result.Dump();
  36. }
  37.  
  38. var result = paths.Select(x =>x.Split(new string [] {"/"},
  39. StringSplitOptions.RemoveEmptyEntries)[0])
  40. .Distinct();
  41.  
  42. var rootList = new List <string>();
  43.  
  44. foreach (var fullPath in myPaths)
  45. {
  46. rootList.Add(Path.GetPathRoot(fullPath))
  47. }
  48.  
  49. return rootList.Distinct();
  50.  
  51. myPaths.Select(x => Path.GetPathRoot(x)).Distinct();
  52.  
  53. myPaths.Select(x => Directory.GetDirectoryRoot(x)).Distinct();
  54.  
  55. string dir = @"C:Level1Level2;
  56.  
  57. string root = Path.GetPathRoot(dir);
  58.  
  59. string pathWithoutRoot = dir.Substring(root.Length);
  60.  
  61. string firstDir = pathWithoutRoot.Split(Path.DirectorySeparatorChar).First();
  62.  
  63. string currentDirectory = "/";
  64. var distinctDirectories = paths.Where(p => p.StartsWith(currentDirectory)
  65. .Select(p => GetFirstSubDir(p, currentDirectory)).Distinct();
  66.  
  67.  
  68. ...
  69.  
  70.  
  71. string GetFirstSubDir(string path, string currentDirectory)
  72. {
  73. int index = path.IndexOf('/', currentDirectory.Length);
  74. if (index >= 0)
  75. return path.SubString(currentDirectory.Length - 1, index + 1 - currentDirectory.Length);
  76. return path.SubString(currentDirectory.Length - 1);
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement