Search code examples
c#linuxmono

Handling external media in C#/Mono on Linux


What I'm wanting to do is detect if an external memory stick is present in a Raspberry-Pi USB socket and then try to read or write files to it. The reading or writing is straightforward (StreamReader etc) but the actual path isn't clear.

On Windows it'd be D:, E: etc. I've looked a lot but information about handling external devices in code seems hard to find.


Solution

  • You can use method System.IO.DriveInfo.GetDrives which return DriveInfo[]

    DriveInfo also has a DriveType property of type enum that might come in useful:

    CDRom 5 The drive is an optical disc device, such as a CD or DVD-ROM.

    Fixed 3 The drive is a fixed disk.

    Network 4 The drive is a network drive.

    NoRootDirectory 1 The drive does not have a root directory.

    Ram 6 The drive is a RAM disk.

    Removable 2 The drive is a removable storage device, such as a USB flash drive.

    Unknown 0 The type of drive is unknown.