current property

Directory get current

Creates a directory object pointing to the current working directory.

Implementation

static Directory get current {
  final IOOverrides? overrides = IOOverrides.current;
  if (overrides == null) {
    return _Directory.current;
  }
  return overrides.getCurrentDirectory();
}
set current (dynamic path)

Sets the current working directory of the Dart process.

This affects all running isolates. The new value set can be either a Directory or a String.

The new value is passed to the OS's system call unchanged, so a relative path passed as the new working directory will be resolved by the OS.

Note that setting the current working directory is a synchronous operation and that it changes the working directory of all isolates.

Use this with care — especially when working with asynchronous operations and multiple isolates. Changing the working directory, while asynchronous operations are pending or when other isolates are working with the file system, can lead to unexpected results.

Implementation

static void set current(dynamic path) {
  // Disallow implicit casts to avoid bugs like
  // <https://github.com/dart-lang/sdk/issues/52140>.
  //
  // This can be removed if `strict-casts` is enabled.
  path as Object?;

  final IOOverrides? overrides = IOOverrides.current;
  if (overrides == null) {
    _Directory.current = path;
    return;
  }

  // IOOverrides.setCurrentDirectory accepts only a [String].
  overrides.setCurrentDirectory(switch (path) {
    String s => s,
    Directory d => d.path,
    _ =>
      throw ArgumentError(
        '${Error.safeToString(path)} is not a String or'
        ' Directory',
      ),
  });
}