private static string FindDownForDirectoryContainingFile(string searchPattern, string rootDirectoryToLookIn, int searchDepth) { if (_searchedDirectories.Contains(rootDirectoryToLookIn + searchPattern)) { return(null); } _searchedDirectories.Add(rootDirectoryToLookIn + searchPattern); if (!Directory.Exists(rootDirectoryToLookIn)) { return(null); } LoggerServer.Log("Searching {0}", rootDirectoryToLookIn); if (EatPermissionErrors(() => Directory.EnumerateFiles(rootDirectoryToLookIn, searchPattern).Any())) { return(rootDirectoryToLookIn); } if (searchDepth > 0) { searchDepth--; foreach (var dir in EatPermissionErrors(() => Directory.GetDirectories(rootDirectoryToLookIn))) { var result = FindDownForDirectoryContainingFile(searchPattern, dir, searchDepth); if (result != null) { return(result); } } } return(null); }
private static T EatPermissionErrors <T>(Func <T> method) { try { return(method.Invoke()); } catch (UnauthorizedAccessException unauthorizedAccessException) { LoggerServer.Log("Hit a directory permission error {0}", unauthorizedAccessException.Message); return((T)Activator.CreateInstance(typeof(T))); } }
/// <summary> /// Based on startDirectory, search up and down for directory that contains a file matching searchPattern. /// </summary> /// <param name="searchPattern"> DOS style search pattern (i.e. *.config) </param> /// <param name="startDirectory"> Starting folder for the search </param> /// <param name="searchDepth"> How many folders to look down at each level (if searchHeight > 0, we will search all children of the parent to this depth </param> /// <param name="searchHeight"> How far above the starting folder should we look </param> /// <returns> Full path of directory. Null if not found. </returns> public static string FindDirectoryContainingFile(string searchPattern, string startDirectory, int searchDepth, int searchHeight = 0) { LoggerServer.Log("Looking for {0} in {1}", searchPattern, startDirectory); string match = null; var dir = startDirectory; while (match == null && dir != null) { match = FindDownForDirectoryContainingFile(searchPattern, dir, searchDepth); var parent = searchHeight > 0 ? EatPermissionErrors(() => Directory.GetParent(dir).FullName) : null; if (searchHeight > 0) { searchHeight--; } dir = parent; } LoggerServer.Log("Found {0} in {1}", searchPattern, match); LoggerServer.Log(""); return(match); }
public byte[] GetAssembly(string assemblyName) { try { // If the assembly is already loaded, use that var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var matchingAssembly = assemblies.FirstOrDefault(x => x.FullName == assemblyName); // Otherwise, look on disk if (matchingAssembly == null) { matchingAssembly = AppDomain.CurrentDomain.Load(assemblyName); } // Now return the assembly bytes if (matchingAssembly != null && matchingAssembly.Location != null) { LoggerServer.Log("Assembly found by AssemblyProvider {0}", matchingAssembly.FullName); return(File.ReadAllBytes(matchingAssembly.Location)); } } catch (FileNotFoundException) { /* Means the assembly wasn't known */ } return(null); }