PluginPathUtils.cs
1 namespace Utils 2 { 3 public static class PluginPathUtils 4 { 5 private const string ProjectPluginsFolderName = "ProjectPlugins"; 6 private static string projectPluginsDir = string.Empty; 7 8 public static string ProjectPluginsDir 9 { 10 get 11 { 12 if (string.IsNullOrEmpty(projectPluginsDir)) projectPluginsDir = FindProjectPluginsDir(); 13 return projectPluginsDir; 14 } 15 } 16 17 private static string FindProjectPluginsDir() 18 { 19 var current = Directory.GetCurrentDirectory(); 20 while (true) 21 { 22 var localFolders = Directory.GetDirectories(current); 23 var projectPluginsFolders = localFolders.Where(l => l.EndsWith(ProjectPluginsFolderName)).ToArray(); 24 if (projectPluginsFolders.Length == 1) 25 { 26 return projectPluginsFolders.Single(); 27 } 28 29 var parent = Directory.GetParent(current); 30 if (parent == null) 31 { 32 var msg = $"Unable to locate '{ProjectPluginsFolderName}' folder. Travelled up from: '{Directory.GetCurrentDirectory()}'"; 33 Console.WriteLine(msg); 34 throw new Exception(msg); 35 } 36 37 current = parent.FullName; 38 } 39 } 40 } 41 }