Table of Contents

Class DirAccess

Namespace
Godot
Assembly
GodotSharp.dll

This class is used to manage directories and their content, even outside of the project folder.

DirAccess can't be instantiated directly. Instead it is created with a static method that takes a path for which it will be opened.

Most of the methods have a static alternative that can be used without creating a DirAccess. Static methods only support absolute paths (including res:// and user://).

# Standard
  var dir = DirAccess.open("user://levels")
  dir.make_dir("world1")
  # Static
  DirAccess.make_dir_absolute("user://levels/world1")

Note: Many resources types are imported (e.g. textures or sound files), and their source asset will not be included in the exported game, as only the imported version is used. Use ResourceLoader to access imported resources.

Here is an example on how to iterate through the files of a directory:

public void DirContents(string path)
  {
      using var dir = DirAccess.Open(path);
      if (dir != null)
      {
          dir.ListDirBegin();
          string fileName = dir.GetNext();
          while (fileName != "")
          {
              if (dir.CurrentIsDir())
              {
                  GD.Print($"Found directory: {fileName}");
              }
              else
              {
                  GD.Print($"Found file: {fileName}");
              }
              fileName = dir.GetNext();
          }
      }
      else
      {
          GD.Print("An error occurred when trying to access the path.");
      }
  }
public class DirAccess : RefCounted, IDisposable
Inheritance
DirAccess
Implements
Inherited Members

Properties

IncludeHidden

If true, hidden files are included when navigating the directory.

Affects ListDirBegin(), GetDirectories() and GetFiles().

public bool IncludeHidden { get; set; }

Property Value

bool

IncludeNavigational

If true, . and .. are included when navigating the directory.

Affects ListDirBegin() and GetDirectories().

public bool IncludeNavigational { get; set; }

Property Value

bool

Methods

ChangeDir(string)

Changes the currently opened directory to the one passed as an argument. The argument can be relative to the current directory (e.g. newdir or ../newdir), or an absolute path (e.g. /tmp/newdir or res://somedir/newdir).

Returns one of the Error code constants (Ok on success).

Note: The new directory must be within the same scope, e.g. when you had opened a directory inside res://, you can't change it to user:// directory. If you need to open a directory in another access scope, use Open(string) to create a new instance instead.

public Error ChangeDir(string toDir)

Parameters

toDir string

Returns

Error

Copy(string, string, int)

Copies the from file to the to destination. Both arguments should be paths to files, either relative or absolute. If the destination file exists and is not access-protected, it will be overwritten.

If chmodFlags is different than -1, the Unix permissions for the destination path will be set to the provided value, if available on the current operating system.

Returns one of the Error code constants (Ok on success).

public Error Copy(string from, string to, int chmodFlags = -1)

Parameters

from string
to string
chmodFlags int

Returns

Error

CopyAbsolute(string, string, int)

Static version of Copy(string, string, int). Supports only absolute paths.

public static Error CopyAbsolute(string from, string to, int chmodFlags = -1)

Parameters

from string
to string
chmodFlags int

Returns

Error

CurrentIsDir()

Returns whether the current item processed with the last GetNext() call is a directory (. and .. are considered directories).

public bool CurrentIsDir()

Returns

bool

DirExists(string)

Returns whether the target directory exists. The argument can be relative to the current directory, or an absolute path.

public bool DirExists(string path)

Parameters

path string

Returns

bool

DirExistsAbsolute(string)

Static version of DirExists(string). Supports only absolute paths.

public static bool DirExistsAbsolute(string path)

Parameters

path string

Returns

bool

FileExists(string)

Returns whether the target file exists. The argument can be relative to the current directory, or an absolute path.

For a static equivalent, use FileExists(string).

public bool FileExists(string path)

Parameters

path string

Returns

bool

GetCurrentDir(bool)

Returns the absolute path to the currently opened directory (e.g. res://folder or C:\tmp\folder).

public string GetCurrentDir(bool includeDrive = true)

Parameters

includeDrive bool

Returns

string

GetCurrentDrive()

Returns the currently opened directory's drive index. See GetDriveName(int) to convert returned index to the name of the drive.

public int GetCurrentDrive()

Returns

int

GetDirectories()

Returns a string[] containing filenames of the directory contents, excluding files. The array is sorted alphabetically.

Affected by IncludeHidden and IncludeNavigational.

public string[] GetDirectories()

Returns

string[]

GetDirectoriesAt(string)

Returns a string[] containing filenames of the directory contents, excluding files, at the given path. The array is sorted alphabetically.

Use GetDirectories() if you want more control of what gets included.

public static string[] GetDirectoriesAt(string path)

Parameters

path string

Returns

string[]

GetDriveCount()

On Windows, returns the number of drives (partitions) mounted on the current filesystem.

On macOS, returns the number of mounted volumes.

On Linux, returns the number of mounted volumes and GTK 3 bookmarks.

On other platforms, the method returns 0.

public static int GetDriveCount()

Returns

int

GetDriveName(int)

On Windows, returns the name of the drive (partition) passed as an argument (e.g. C:).

On macOS, returns the path to the mounted volume passed as an argument.

On Linux, returns the path to the mounted volume or GTK 3 bookmark passed as an argument.

On other platforms, or if the requested drive does not exist, the method returns an empty String.

public static string GetDriveName(int idx)

Parameters

idx int

Returns

string

GetFiles()

Returns a string[] containing filenames of the directory contents, excluding directories. The array is sorted alphabetically.

Affected by IncludeHidden.

Note: When used on a res:// path in an exported project, only the files actually included in the PCK at the given folder level are returned. In practice, this means that since imported resources are stored in a top-level .godot/ folder, only paths to *.gd and *.import files are returned (plus a few files such as project.godot or project.binary and the project icon). In an exported project, the list of returned files will also vary depending on whether ProjectSettings.editor/export/convert_text_resources_to_binary is true.

public string[] GetFiles()

Returns

string[]

GetFilesAt(string)

Returns a string[] containing filenames of the directory contents, excluding directories, at the given path. The array is sorted alphabetically.

Use GetFiles() if you want more control of what gets included.

public static string[] GetFilesAt(string path)

Parameters

path string

Returns

string[]

GetNext()

Returns the next element (file or directory) in the current directory.

The name of the file or directory is returned (and not its full path). Once the stream has been fully processed, the method returns an empty string and closes the stream automatically (i.e. ListDirEnd() would not be mandatory in such a case).

public string GetNext()

Returns

string

GetOpenError()

Returns the result of the last Open(string) call in the current thread.

public static Error GetOpenError()

Returns

Error

GetSpaceLeft()

Returns the available space on the current directory's disk, in bytes. Returns 0 if the platform-specific method to query the available space fails.

public ulong GetSpaceLeft()

Returns

ulong

HasGodotClassMethod(in godot_string_name)

Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.

protected override bool HasGodotClassMethod(in godot_string_name method)

Parameters

method godot_string_name

Name of the method to check for.

Returns

bool

HasGodotClassSignal(in godot_string_name)

Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.

protected override bool HasGodotClassSignal(in godot_string_name signal)

Parameters

signal godot_string_name

Name of the signal to check for.

Returns

bool

InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)

Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.

protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)

Parameters

method godot_string_name

Name of the method to invoke.

args NativeVariantPtrArgs

Arguments to use with the invoked method.

ret godot_variant

Value returned by the invoked method.

Returns

bool

IsCaseSensitive(string)

Returns true if the file system or directory use case sensitive file names.

Note: This method is implemented on macOS, Linux (for EXT4 and F2FS filesystems only) and Windows. On other platforms, it always returns true.

public bool IsCaseSensitive(string path)

Parameters

path string

Returns

bool

ListDirBegin()

Initializes the stream used to list all files and directories using the GetNext() function, closing the currently opened stream if needed. Once the stream has been processed, it should typically be closed with ListDirEnd().

Affected by IncludeHidden and IncludeNavigational.

Note: The order of files and directories returned by this method is not deterministic, and can vary between operating systems. If you want a list of all files or folders sorted alphabetically, use GetFiles() or GetDirectories().

public Error ListDirBegin()

Returns

Error

ListDirEnd()

Closes the current stream opened with ListDirBegin() (whether it has been fully processed with GetNext() does not matter).

public void ListDirEnd()

MakeDir(string)

Creates a directory. The argument can be relative to the current directory, or an absolute path. The target directory should be placed in an already existing directory (to create the full path recursively, see MakeDirRecursive(string)).

Returns one of the Error code constants (Ok on success).

public Error MakeDir(string path)

Parameters

path string

Returns

Error

MakeDirAbsolute(string)

Static version of MakeDir(string). Supports only absolute paths.

public static Error MakeDirAbsolute(string path)

Parameters

path string

Returns

Error

MakeDirRecursive(string)

Creates a target directory and all necessary intermediate directories in its path, by calling MakeDir(string) recursively. The argument can be relative to the current directory, or an absolute path.

Returns one of the Error code constants (Ok on success).

public Error MakeDirRecursive(string path)

Parameters

path string

Returns

Error

MakeDirRecursiveAbsolute(string)

Static version of MakeDirRecursive(string). Supports only absolute paths.

public static Error MakeDirRecursiveAbsolute(string path)

Parameters

path string

Returns

Error

Open(string)

Creates a new DirAccess object and opens an existing directory of the filesystem. The path argument can be within the project tree (res://folder), the user directory (user://folder) or an absolute path of the user filesystem (e.g. /tmp/folder or C:\tmp\folder).

Returns null if opening the directory failed. You can use GetOpenError() to check the error that occurred.

public static DirAccess Open(string path)

Parameters

path string

Returns

DirAccess

Remove(string)

Permanently deletes the target file or an empty directory. The argument can be relative to the current directory, or an absolute path. If the target directory is not empty, the operation will fail.

If you don't want to delete the file/directory permanently, use MoveToTrash(string) instead.

Returns one of the Error code constants (Ok on success).

public Error Remove(string path)

Parameters

path string

Returns

Error

RemoveAbsolute(string)

Static version of Remove(string). Supports only absolute paths.

public static Error RemoveAbsolute(string path)

Parameters

path string

Returns

Error

Rename(string, string)

Renames (move) the from file or directory to the to destination. Both arguments should be paths to files or directories, either relative or absolute. If the destination file or directory exists and is not access-protected, it will be overwritten.

Returns one of the Error code constants (Ok on success).

public Error Rename(string from, string to)

Parameters

from string
to string

Returns

Error

RenameAbsolute(string, string)

Static version of Rename(string, string). Supports only absolute paths.

public static Error RenameAbsolute(string from, string to)

Parameters

from string
to string

Returns

Error