Folder icon

Access files and folders in Grasshopper with the C# component

It is very easy to delve into the files and folders on your computer in C#. The DirectoryInfo class contains a wealth of methods for reading, creating and modifying files and folders.

Here is an example created in Grasshopper showing how we can use the DirectoryInfo class to get a list of subfolders contained within a folder.

A grasshopper component to read in a file path and return containing folders. Created using the C# component.

Source code

This code takes in a folder path as a string, and returns a list of folder names contained within that folder.

The code is formatted for the C# component in Grasshopper but can be modified to suit any C# application.

  private void RunScript(string dir, ref object dirs)
  {

    System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(dir);
    System.IO.DirectoryInfo[] dirInfos = dirInfo.GetDirectories("*.*");

    List<string> dirNames = new List<string>();

    foreach(System.IO.DirectoryInfo d in dirInfos)
    {
      dirNames.Add(d.Name);
    }

    dirs = dirNames;

  }

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: