Does anyone know of a method to list the physical disks on Windows using Flutter?

I've done a bunch of research but can't find anything. Only the path_provider, but that only provides access to the temp locations.

I suspect this isn't possible... 😢

Solution 1: julemand101

I don't think there are any method in the SDK which provides such a feature. But you can call another executable and parse the result.

In Windows you can get the list of available drives through:

C:\>wmic logicaldisk get caption

So we can create the following in Dart:

import 'dart:convert';
import 'dart:io';

Future<void> main() async {
  print(await getDrivesOnWindows()); // (C:, E:, F:, G:, Z:)

Future<Iterable<String>> getDrivesOnWindows() async => LineSplitter.split(
        (await'wmic', ['logicaldisk', 'get', 'caption'],
                stdoutEncoding: const SystemEncoding()))
            .stdout as String)
    .map((string) => string.trim())
    .where((string) => string.isNotEmpty)

A more complicated example showing how to get multiple values out for each drive. The freeSpace is nullable since unconnected network drives (or empty CD/DVD drives) returns no value when asked for free disk space:

import 'dart:convert';
import 'dart:io';

Future<void> main() async {
  (await WindowsDrive.getDrives()).forEach(print);
  // DriveLetter: C:, FreeSpace: 20392869888
  // DriveLetter: E:, FreeSpace: null
  // DriveLetter: F:, FreeSpace: 69761130496
  // DriveLetter: G:, FreeSpace: 85562028032
  // DriveLetter: Z:, FreeSpace: null

class WindowsDrive {
  final String driveLetter;
  final int? freeSpace;

  const WindowsDrive(this.driveLetter, this.freeSpace);

  // Node,Caption,FreeSpace
  factory WindowsDrive.parse(String line) {
    final segments = line.split(',');
    return WindowsDrive(segments[1], int.tryParse(segments[2]));

  String toString() => 'DriveLetter: $driveLetter, FreeSpace: $freeSpace';

  static Future<Iterable<WindowsDrive>> getDrives() async =>
                  stdoutEncoding: const SystemEncoding()))
              .stdout as String)
          .map((string) => string.trim())
          .where((string) => string.isNotEmpty)
          .map((e) => WindowsDrive.parse(e));