public static IEnumerable<string> QueryDosDevice(string deviceName) { if (deviceName != null) deviceName = Paths.RemoveTrailingSeparators(deviceName); // Null will return everything defined- this list is quite large so set a higher initial allocation using (NativeBuffer buffer = new NativeBuffer(deviceName == null ? (uint)8192 : 256)) { uint result = 0; // QueryDosDevicePrivate takes the buffer count in TCHARs, which is 2 bytes for Unicode (WCHAR) while ((result = QueryDosDevicePrivate(deviceName, buffer, buffer.Size / 2)) == 0) { int lastError = Marshal.GetLastWin32Error(); switch (lastError) { case WinError.ERROR_INSUFFICIENT_BUFFER: buffer.Resize(buffer.Size * 2); break; default: throw GetIoExceptionForError(lastError, deviceName); } } return Strings.Split(buffer, (int)result - 2, '\0'); } }
internal static IEnumerable<string> GetLogicalDriveStrings() { using (NativeBuffer buffer = new NativeBuffer()) { uint result = 0; // GetLogicalDriveStringsPrivate takes the buffer count in TCHARs, which is 2 bytes for Unicode (WCHAR) while ((result = GetLogicalDriveStringsPrivate((uint)buffer.Size / 2, buffer)) > buffer.Size / 2) { buffer.Resize(result * 2); } if (result == 0) { int lastError = Marshal.GetLastWin32Error(); throw GetIoExceptionForError(lastError); } return Strings.Split(buffer, (int)result - 1, '\0'); } }
internal static IEnumerable<string> GetVolumePathNamesForVolumeName(string volumeName) { using (NativeBuffer buffer = new NativeBuffer()) { uint returnLength = 0; // GetLogicalDriveStringsPrivate takes the buffer count in TCHARs, which is 2 bytes for Unicode (WCHAR) while (!GetVolumePathNamesForVolumeNamePrivate(volumeName, buffer, (uint)buffer.Size / 2, ref returnLength)) { int lastError = Marshal.GetLastWin32Error(); switch (lastError) { case WinError.ERROR_MORE_DATA: buffer.Resize(returnLength * 2); break; default: throw GetIoExceptionForError(lastError, volumeName); } } return Strings.Split(buffer, (int)returnLength - 2, '\0'); } }