我正在编写一个安装程序来将应用程序安装到 USB 驱动器。该应用程序只能从 USB 驱动器使用,因此它会通过自动选择要安装的 USB 驱动器为用户节省一个额外的步骤。
我可能会探索使用 Nullsoft 或 MSI 进行安装,但由于我最熟悉 .NET,我最初计划在 .NET 上尝试自定义 .NET 安装程序或设置组件。
是否可以使用 .NET 在 Windows 上确定 USB 闪存驱动器的驱动器号?如何?
你可以使用:
from driveInfo in DriveInfo.GetDrives()
where driveInfo.DriveType == DriveType.Removable && driveInfo.IsReady
select driveInfo.RootDirectory.FullName
这将枚举系统上没有 LINQ 但仍使用 WMI 的所有驱动器:
// browse all USB WMI physical disks
foreach(ManagementObject drive in new ManagementObjectSearcher(
"select * from Win32_DiskDrive where InterfaceType='USB'").Get())
{
// associate physical disks with partitions
foreach(ManagementObject partition in new ManagementObjectSearcher(
"ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + drive["DeviceID"]
+ "'} WHERE AssocClass =
Win32_DiskDriveToDiskPartition").Get())
{
Console.WriteLine("Partition=" + partition["Name"]);
// associate partitions with logical disks (drive letter volumes)
foreach(ManagementObject disk in new ManagementObjectSearcher(
"ASSOCIATORS OF {Win32_DiskPartition.DeviceID='"
+ partition["DeviceID"]
+ "'} WHERE AssocClass =
Win32_LogicalDiskToPartition").Get())
{
Console.WriteLine("Disk=" + disk["Name"]);
}
}
// this may display nothing if the physical disk
// does not have a hardware serial number
Console.WriteLine("Serial="
+ new ManagementObject("Win32_PhysicalMedia.Tag='"
+ drive["DeviceID"] + "'")["SerialNumber"]);
}
C# 2.0 版本的 Kent 代码(从我的头顶,未测试):
IList<String> fullNames = new List<String>();
foreach (DriveInfo driveInfo in DriveInfo.GetDrives()) {
if (driveInfo.DriveType == DriveType.Removable) {
fullNames.Add(driveInfo.RootDirectory.FullName);
}
}