How to get the drive letter of an SD Card connected to a PC, from a C# .NET Framework application?

Jeff Nygren 121 Reputation points
2021-06-22T15:15:51.46+00:00

Using System.IO.DriveInfo.GetDrives() or System.Management.ManagementObjectSearcher() with query "Win32_LogicalDisk", I can get the drive letters of all devices, but I can't tell which device(s) is/are the SD card(s).
Using System.Management.ManagementObjectSearcher() with query "CIM_LogicalDevice", "Caption = 'SDHC Card'", I get 2 devices with the "SDHC Card" caption property, but no drive letters. (See attached sample code.)
108225-programcs.txt
How can I get the drive letter of the SD Card or card reader?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,199 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,117 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Sam of Simple Samples 5,516 Reputation points
    2021-06-22T18:06:14.31+00:00

    This is where the WMI Explorer that I told you about in my answer in your previous question helps. The following is the query in my previous answer:

    SELECT * FROM Win32_DiskDrive Where InterfaceType='USB'
    

    When I put that query into WMI Explorer I see that Model is SD Card. That is what you are looking for, correct?

    When I put:

    SELECT * FROM Win32_DiskDrive Where InterfaceType='USB'
    

    and format using:

    sb.Append($"{drive["caption"]} | {drive["MediaType"]} | {drive["Model"]}\r\n");
    

    into the sample code in my answer in your previous question I get:

    Seagate Portable USB Device | External hard disk media | Seagate Portable USB Device
    SDHC Card | Removable Media | SDHC Card
    

  2. Jeff Nygren 121 Reputation points
    2021-06-24T20:32:34.497+00:00

    I have found the answer to my question, thanks to the help from @Sam of Simple Samples and @Daniel Zhang-MSFT .

    Here is a class that lets you get the drive letter of an SD Card connected to a PC:

    109107-getsdcardscs.txt

    0 comments No comments