dart-windows / win32

Build Win32 apps with Dart!
https://win32.pub
BSD 3-Clause "New" or "Revised" License
735 stars 118 forks source link

How can I get this info using the Win 32 package? #802

Closed DDXBIZ closed 6 months ago

DDXBIZ commented 6 months ago

How do I get the Volume Name for each Drive using win32 ? is this possible? would you have an example?

I have tried the explorer example app, but I cannot get this volume name...

sample storage drive

Please advice.

halildurmus commented 6 months ago

Here's a simple example of obtaining the volume name of a drive:

import 'dart:ffi';

import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart';

void main() {
  // Specify the root directory of the volume (e.g., 'C:\')
  const rootPath = r'C:\';

  final lpRootName = rootPath.toNativeUtf16();
  final lpVolumeName = wsalloc(MAX_PATH + 1);

  if (GetVolumeInformation(lpRootName, lpVolumeName, MAX_PATH + 1, nullptr,
          nullptr, nullptr, nullptr, NULL) !=
      NULL) {
    final volumeName = lpVolumeName.toDartString();
    print('$rootPath volume name: $volumeName');
  } else {
    print('Error getting volume information.');
  }

  free(lpVolumeName);
  free(lpRootName);
}
DDXBIZ commented 6 months ago

Thanks. This is a great Help.