As I say, this is easy to do via
System.Management
.
Naturally, there is no such such concept as "thumb drive". There is a concept of removable drive. This is a drive with
DriveType
2. You need to scan all available volumes and find out a removable drive, or a set of drives. Then you can unmount one or more of them. When this is done, OS guarantees that you can safely remove the drive/card, because all previously uncommitted is not committed.
The management object you need to obtain is
Win32_Volume
. All volumes are obtained via the WMI query "
SELECT * FROM Win32_Volume WHERE ...
".
Please also see this CodeProject article:
Eject USB disks using C#[
^].
[EDIT]
Answering follow-up questions:
You don't have access to
Win32_Volume
directly as it would be a .NET class. WMI gives you reflective interface to them. (Not to mix up with .NET reflection.) You can get properties and methods my names and invoke them (well, unfortunately, this is how it's done).
For example, here is how to dismount removable drives:
using System;
using System.Management;
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(
"SELECT * FROM Win32_Volume WHERE DriveType=2");
ManagementObjectCollection collection = searcher.Get();
foreach (ManagementObject item in collection) {
System.Console.WriteLine("Name: {0}", item["Name"]);
var result =
item.InvokeMethod("Dismount",
new object[] { false, false });
if (result != 0) {}
}
You can get documentation for
Win32_Volume
separately, from C++ API and call methods/properties by their documented names.
—SA