> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/flatredball/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/flatredball/api/flatredball/io/filemanager/getallfilesindirectory.md).

# GetAllFilesInDirectory

### Introduction

GetAllFilesInDirectory is a method which can be used to get all files in a given directory and its sub-directories. The overloads for this method provide constraints such as the number of levels deep to search, and limitations on the file types.

### Overloads

```
public static List<string> GetAllFilesInDirectory(string directory)
public static List<string> GetAllFilesInDirectory(string directory, string fileType)
public static List<string> GetAllFilesInDirectory(string directory, string fileType, int depthToSearch)
public static void GetAllFilesInDirectory(string directory, string fileType, int depthToSearch, List<string> arrayToReturn)
```

The following call:

```
List<string> result = GetAllFilesInDirectory(directoryName);
```

is equivalent to calling:

```
List<string> result = GetAllFilesInDirectory(directoryName, "", int.MaxValue)
```

An empty string as the fileType will return all contained files.

### Code Example

The following code creates a [ListBox](https://github.com/flatredball/FlatRedBallDocs/tree/main/frb/docs/index.php) to display all .jpg files which exist in the c:\Projects directory. If you do not have this directory on your computer, you will need to change the directory variable. Keep in mind that if the depth variable is large, or if the argument directory has a lot of files and sub-directories, this call can take a long time to execute. Add the following using statements:

```
using FlatRedBall.IO;
using FlatRedBall.Gui;
```

Add the following to Initialize after initializing FlatRedBall:

```
GuiManager.IsUIEnabled = true;
IsMouseVisible = true;

ListBox listBox = GuiManager.AddListBox();
listBox.ScaleX = 33;
listBox.ScaleY = 25;

string directory = @"c:\Projects";
string fileType = "jpg"; // could be empty for all files
int depth = 3; // the number of folders deep to search

List<string> allFiles = FileManager.GetAllFilesInDirectory(
    directory, fileType, depth);

foreach (string s in allFiles)
{
    listBox.AddItem(s);
}
```

![GetAllFilesInDirectory.png](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-f538940ff3a77f854d15c9645ed0c43063dbf727%2Fmigrated_media-GetAllFilesInDirectory.png?alt=media)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.flatredball.com/flatredball/api/flatredball/io/filemanager/getallfilesindirectory.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
