/// <summary> /// Gets all of the paths, along with their lengths, as a string. /// </summary> /// <param name="options">The options.</param> public static string GetPathsWithLengthsAsString(PathLengthSearchOptions options) { var text = new StringBuilder(); foreach (var path in GetPathsWithLengths(options)) { text.AppendLine(path.ToString()); } return(text.ToString()); }
/// <summary> /// Gets the paths with lengths. /// </summary> /// <param name="options">The options.</param> public static IEnumerable <PathInfo> GetPathsWithLengths(PathLengthSearchOptions options) { foreach (var path in GetPaths(options)) { yield return(new PathInfo() { Path = path }); } }
/// <summary> /// Gets all of the paths, along with their lengths, as a string. /// </summary> /// <param name="options">The options.</param> public static string GetPathsWithLengthsAsString(PathLengthSearchOptions options, CancellationToken cancellationToken) { var text = new StringBuilder(); foreach (var path in GetPathsWithLengths(options, cancellationToken)) { text.AppendLine($"{path.Length}: {path.Path}"); } return(text.ToString()); }
/// <summary> /// Gets the paths with lengths. /// </summary> /// <param name="options">The options.</param> public static IEnumerable <PathInfo> GetPathsWithLengths(PathLengthSearchOptions options, CancellationToken cancellationToken) { foreach (var path in RetrievePaths(options, cancellationToken)) { yield return(new PathInfo() { Path = path }); } }
/// <summary> /// Application entry point. /// </summary> /// <param name="args">The command line arguments.</param> static void Main(string[] args) { try { // Fill the search options from the provided command line arguments. var searchOptions = new PathLengthSearchOptions(); ParseArgs(args, ref searchOptions); if (string.Equals(_output, "MinLength", StringComparison.InvariantCultureIgnoreCase) || string.Equals(_output, "MaxLength", StringComparison.InvariantCultureIgnoreCase)) { // Do the search. var paths = PathLengthChecker.GetPathsWithLengths(searchOptions); // Output the desired information (Min Path, Max Path, or All Paths). if (string.Equals(_output, "MinLength", StringComparison.InvariantCultureIgnoreCase)) { Console.WriteLine(paths.Min(p => p.Length)); } else if (string.Equals(_output, "MaxLength", StringComparison.InvariantCultureIgnoreCase)) { Console.WriteLine(paths.Max(p => p.Length)); } } else { var paths = PathLengthChecker.GetPathsWithLengthsAsString(searchOptions); Console.WriteLine(paths); } } catch (Exception ex) { // Write any errors that occurred. Console.Error.WriteLine(ex); Console.Error.WriteLine(); PrintUsage(); } finally { // If we're debugging this through visual studio, wait for input before closing so we can see the output. if (Debugger.IsAttached) { Console.ReadKey(); } } }
public static IEnumerable <string> GetPaths(PathLengthSearchOptions options) { // Make sure valid lengths were supplied if (options.MinimumPathLength > options.MaximumPathLength && options.MinimumPathLength >= 0 && options.MaximumPathLength >= 0) { throw new MinPathLengthGreaterThanMaxPathLengthException(); } // Get the paths. var paths = PathRetriever.GetPaths(options); // Filter out paths that don't match the Minimum Path Length foreach (var path in paths.Where(path => path.Length >= options.MinimumPathLength && (options.MaximumPathLength < 0 || path.Length <= options.MaximumPathLength))) { yield return(path); } }
/// <summary> /// Parses the specified args array into a PathLengthSearchOptions object instance. /// </summary> public static PathLengthSearchOptions ParseArgs(IEnumerable <string> args) { var searchOptions = new PathLengthSearchOptions(); foreach (var arg in args) { // Split the command-line arg on the equals sign. var parameter = arg.Split("=".ToCharArray(), 2); if (parameter.Count() < 2) { throw new ArgumentException("All parameters must be of the format 'Parameter=Value'"); } // Assign the Command and Value to temp variables for processing. var command = parameter[0]; var value = parameter[1]; // Fill in the Search Options based on the Command. switch (command) { default: throw new ArgumentException("Unrecognized command: " + command); case "RootDirectory": searchOptions.RootDirectory = value; break; case "RootDirectoryReplacement": searchOptions.RootDirectoryReplacement = string.Equals(value, "null", StringComparison.InvariantCultureIgnoreCase) ? null : value; break; case "SearchOption": searchOptions.SearchOption = string.Equals("TopDirectory", value, StringComparison.InvariantCultureIgnoreCase) ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories; break; case "TypesToInclude": FileSystemTypes typesToInclude = FileSystemTypes.All; if (string.Equals("OnlyFiles", value, StringComparison.InvariantCultureIgnoreCase)) { typesToInclude = FileSystemTypes.Files; } else if (string.Equals("OnlyDirectories", value, StringComparison.InvariantCultureIgnoreCase)) { typesToInclude = FileSystemTypes.Directories; } searchOptions.TypesToGet = typesToInclude; break; case "SearchPattern": searchOptions.SearchPattern = value; break; case "MinLength": int minLength = -1; if (int.TryParse(value, out minLength)) { searchOptions.MinimumPathLength = minLength; } break; case "MaxLength": int maxLength = -1; if (int.TryParse(value, out maxLength)) { searchOptions.MaximumPathLength = maxLength; } break; case "Output": OutputTypes outputType = OutputTypes.Paths; if (Enum.TryParse(value, out outputType)) { searchOptions.OutputType = outputType; } break; } } return(searchOptions); }