Domanda

Sto scrivendo un programma di installazione per installare un'applicazione su un'unità USB. L'applicazione è pensata per essere utilizzata solo da unità USB, quindi risparmierebbe un ulteriore passaggio all'utente selezionando automaticamente l'unità USB su cui installare.

Potrei esplorare usando Nullsoft o MSI per l'installazione, ma poiché ho familiarità con .NET, inizialmente ho intenzione di provare il programma di installazione .NET personalizzato o il componente di installazione su .NET.

È possibile determinare la lettera di unità di un'unità flash USB su Windows usando .NET? Come?

È stato utile?

Soluzione

Puoi usare:

from driveInfo in DriveInfo.GetDrives()
where driveInfo.DriveType == DriveType.Removable && driveInfo.IsReady
select driveInfo.RootDirectory.FullName

Altri suggerimenti

Questo enumera tutte le unità sul sistema senza LINQ ma usando ancora 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"]);
}

Fonte

Versione C # 2.0 del codice di Kent (dalla cima della mia testa, non testato):

IList<String> fullNames = new List<String>();
foreach (DriveInfo driveInfo in DriveInfo.GetDrives()) {
    if (driveInfo.DriveType == DriveType.Removable) {
        fullNames.Add(driveInfo.RootDirectory.FullName);
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top