我正在编写一个安装程序,将应用程序安装到USB驱动器上。该应用程序仅可从USB驱动器中使用,因此通过自动选择要安装到的USB驱动器,可以为用户节省额外的步骤。
我可能会尝试使用Nullsoft或MSI进行安装,但由于我对.NET更熟悉,因此我最初计划尝试使用自定义.NET安装程序或.NET上的安装组件。
在Windows上使用.NET是否可以确定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);
}
}