[gelöst] USB-Stick erkennen
-
Zur Erkennung eines USB-Sticks verwende ich folgende Methode:
private string Laufwerke() { // ----------------------------------------------------------------------- // Laufwerke prüfen, ob USB-Stick ready // ----------------------------------------------------------------------- string lw = ""; string[] drv = Environment.GetLogicalDrives(); bool ready; int type; DateTime time1, time2; time1 = DateTime.Now; // ..................................................... // diese Schleife braucht knapp 6 Sekunden! foreach (string s in drv) { type = -1; DriveInfo di = new DriveInfo(s); ready = di.IsReady; if (ready) { type = (int)di.DriveType; if (type == 2) lw = di.Name; // gefunden } } // .................................................... time2 = DateTime.Now; TimeSpan ts = time2 - time1; MessageBox.Show("Laufwerke\n" + "\nts " + ts.TotalSeconds.ToString() + ""); return lw; }
Die foreach-Schleife braucht mit knapp 6 Sekunden extrem lange. Kann man das
anders schneller machen?
-
Kann das Problem nicht nachvollziehen, bei mir läuft es in knapp 5 Millisekunden.
Welcher Methodenaufruf bei welchem Laufwerk ist denn langsam? Eventuell ist das ein Laufwerk das inaktiv ist und erst gestartet wird.
-
CD-ROM?
Netzlaufwerk?
Vielleicht würde es reichen einfach die Abfrage auf .IsReady wegzulassen (wozu hast du die überhaupt drinnen)?Ansonsten...
Guck dir mal WMI an. Lässt sich mit dem .NET Framework super einfach verwenden. Und von derWin32_DiskDrive
solltest du alles bekommen was du brauchst:instance of Win32_DiskDrive { BytesPerSector = 512; Capabilities = {3, 4, 7}; CapabilityDescriptions = {"Random Access", "Supports Writing", "Supports Removable Media"}; Caption = "JetFlash Transcend 8GB USB Device"; ConfigManagerErrorCode = 0; ConfigManagerUserConfig = FALSE; CreationClassName = "Win32_DiskDrive"; Description = "Disk drive"; DeviceID = "\\\\.\\PHYSICALDRIVE7"; FirmwareRevision = "8.07"; Index = 7; InterfaceType = "USB"; Manufacturer = "(Standard disk drives)"; MediaLoaded = TRUE; MediaType = "Removable Media"; Model = "JetFlash Transcend 8GB USB Device"; Name = "\\\\.\\PHYSICALDRIVE7"; Partitions = 1; PNPDeviceID = "USBSTOR\\DISK&VEN_JETFLASH&PROD_TRANSCEND_8GB&REV_8.07\\8EKENQLU&0"; SCSIBus = 0; SCSILogicalUnit = 0; SCSIPort = 0; SCSITargetId = 0; SectorsPerTrack = 63; SerialNumber = "8EKENQLU"; Signature = 0; Size = "8027873280"; Status = "OK"; SystemCreationClassName = "Win32_ComputerSystem"; SystemName = "STRENGGEHAIM!!!"; TotalCylinders = "976"; TotalHeads = 255; TotalSectors = "15679440"; TotalTracks = "248880"; TracksPerCylinder = 255; };
OK, nicht alles, vermutlich willst du nen Laufwerksbuchstaben.
Den kannst du aber über die "associators" bekommen.
Von derWin32_DiskDrive
kommst du an ne Liste vonWin32_DiskPartition
, und von derWin32_DiskPartition
kommst du an neWin32_LogicalDisk
.
-
Danke für die Hinweise!
Der ursprüngliche Ansatz brauchte nur auf einem PC so lange, auf anderen nicht.
Mit dem neuen Ansatz ist das Problem vollständig gelöst:private string Laufwerke() { // ----------------------------------------------------------------------- // Laufwerke prüfen, ob USB-Stick vorhanden // ----------------------------------------------------------------------- string lw = ""; string[] drv = Environment.GetLogicalDrives(); int type; foreach (string s in drv) { type = -1; DriveInfo di = new DriveInfo(s); type = (int)di.DriveType; if (type == 2) { lw = di.Name; // Laufwerk A:\ ausschliessen if (String.Compare(lw, "A:\\") == 0) lw = ""; } } return lw; }