Search code examples
c#directorycriteriasystem.diagnosticsprocess.start

C# Directory.GetDirectories Output to string


string[] directory = Directory.GetDirectories(path, Criteria)

Using the above and System.Diagnostics.Process.Start(directory) I can open up multiple directory that meet certain Criteria - the criteria being the name of the folder in a directory.

How do I take the output of the System.Diagnostics.Process.Start(directory) and convert to a string, rather than open the directory?

All the subfolders within the directories that would open, have the same folder structure, so I would like to navigate to specific subfolders within these directories; the only way I can think of doing that, is by redirecting the System.Diagnostics.Process.Start(directory) output to a string, and adding the rest of folder path such as @'\Photos' to the output string and then using System.Diagnostics.Process.Start(newPath); on this new folder path.

If anyone has a better way of doing it I'm all ears. My full code is below:

string path = @"C:\Projects\";
string criteria = "*" + textBox1.Text + "*";

string[] dir = Directory.GetDirectories(path, criteria);
foreach (string directory in dir)
      {
          System.Diagnostics.Process.Start(directory);
      }

Solution

  • To get the list of folders within a folder, you can use the same method Directory.GetDirectories()

    string path = @"C:\Projects\";
    string criteria = "*" + textBox1.Text + "*";
    
    string[] dir = Directory.GetDirectories(path, criteria);
    foreach (string directory in dir)
    {
        string newCriteria = "Photos";
        string[] subDir = Directory.GetDirectories(directory, newCriteria);
        foreach (string subDirectory in subDir)
        {
            System.Diagnostics.Process.Start(subDirectory);
        }
    }