Get available drive letter and mount

Agha Khan 161 Reputation points

public PARTITION_STYLE PartitionStyle;
public int PartitionCount;
public DRIVE_LAYOUT_INFORMATION_UNION DriveLayoutInformaiton;

        [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = 0x16)]  
        public PARTITION_INFORMATION_EX[] PartitionEntry;  

var driveList = DriveInfo.GetDrives();

        bool foundSystemDrive = false;  
        int finalDiskIndex = -1;  
        for (int diskIndex = 0; diskIndex < driveList.Length; diskIndex++)  
            if (!foundSystemDrive)  
                IntPtr hDisk = CreateFile(@$"\\.\PhysicalDrive{diskIndex}", GENERIC_READ,  
                    FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero,  
                    OPEN_EXISTING, 0, IntPtr.Zero);  
                if (hDisk != INVALID_HANDLE_VALUE)  
                        uint lpBytesReturned;  
                        var fSuccess = DeviceIoControl(hDisk, IOCTL_DISK_GET_DRIVE_LAYOUT_EX, IntPtr.Zero, 0,  
                            ref dli, (uint)Marshal.SizeOf(dli), out lpBytesReturned, IntPtr.Zero);  
                        if (fSuccess)  
                            var partitionEntryList = dli.PartitionEntry;  
                            string driveInfo = $"Drive has {partitionEntryList} partitions";  
                            string driveStyle = $"Drive partition style is {partitionEntryList}";  
                            string singlePartiton = string.Empty;  
                            foreach (var obj in partitionEntryList)  
                                efiSystemPartition = obj.DriveLayoutInformaiton.Gpt.Name == "EFI system partition";  
                                partitionIndex = obj.PartitionNumber;  
                                singlePartiton =  
                                    $"Partition number is {obj.PartitionNumber}, style is {obj.PartitionStyle}, size is {obj.PartitionLength} bytes";  
                                if (efiSystemPartition)  
                                    foundSystemDrive = true;  
                                    finalDiskIndex = diskIndex;  
                            string message = $"DeviceIoControl error code as {Marshal.GetLastWin32Error()}";  
                    catch (Exception ex)  


This code gives me information that I have EFI partition at Desk #0 and partition #0.
Now I want to write some data at EFI partition but need a drive letter to the mountain it.
I need a function that tells me which drive letters are available. I just can’t assume that Z letter is available. (Z may be a network drive)

How can I ask Mountvol.exe that I am interested in Desk #0 and partition #0?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,446 questions
{count} votes

5 answers

Sort by: Most helpful
  1. Castorix31 82,036 Reputation points

    I need a function that tells me which drive letters are available.

    GetLogicalDrives or GetLogicalDriveStrings ?

    0 comments No comments

  2. Agha Khan 161 Reputation points

    Hello @Castorix31 :
    They are Logical drives and do not give information about which letters are available. Suppose you have a Network drive and a letter is assigned to it. You can not use that letter. I could also check network drives,

  3. Xiaopo Yang - MSFT 11,506 Reputation points Microsoft Vendor

    For Network Drives, you can use WNetEnumResource to enumerate network resources or existing connections. For an example, see Enumerating Network Resources.

    0 comments No comments

  4. Agha Khan 161 Reputation points

    @ XiaopoYang-MSFT · Looks good, but I am working with C#. Do you any code in C#.

  5. Agha Khan 161 Reputation points

    Hi @Xiaopo Yang - MSFT :
    // C++ solution
    std::list<char> driveList;
    auto drives = GetLogicalDrives();
    DWORD mask = 0x00000001;

    for (int i = 0; i < 32; i++, mask <<= 1)
    if ((drives & mask) != 0)
    driveList.push_back(i + _TEXT('A'));
    It will give you net drives and all logical drives.