public static UsbDriveInfo LoadUsbFromDriveLetter(char driveLetter) { const int maxAttempts = 5; try { if (char.IsLetter(driveLetter)) { for (int attempts = 0; attempts < maxAttempts; ++attempts) { try { ManagementObject logicalObj = GetLogicalObjectFromDriveLetter(driveLetter); logicalObj.Get(); ManagementObject diskObj = GetDiskObjectFromLogical(logicalObj); string volumeName = logicalObj["VolumeName"].ToString(); object serialNumberObject = logicalObj["VolumeSerialNumber"]; string modelName = diskObj["Model"].ToString(); object pnpDeviceId = diskObj["PNPDeviceID"]; string uniqueId = FormatDriveUniqueID(serialNumberObject != null ? serialNumberObject.ToString() : "", pnpDeviceId != null ? pnpDeviceId.ToString() : ""); UsbDriveInfo driveInfo = UsbDriveList.Instance.GetUsbInfoByUniqueId(uniqueId); if (driveInfo == null) { driveInfo = UsbDriveList.Instance.CreateEmptyUsbInfo(modelName, volumeName, uniqueId); } string sizeString = logicalObj["Size"].ToString(); System.UInt64 sizeBytes = 0; System.UInt64.TryParse(sizeString, out sizeBytes); driveInfo.__MarkActive(driveLetter, sizeBytes); diskObj.Dispose(); logicalObj.Dispose(); return(driveInfo); } catch (System.Exception) { // failed, try again until maxAttempts //UsbDriveList.__LogError( null, "LoadUsbFromDriveLetter attempt failed : " + (attempts + 1) + "/" + maxAttempts, e ); System.Threading.Thread.Sleep(200); } } } else { throw new Exception("Drive letter is not valid " + driveLetter.ToString()); } } catch (System.Exception e) { UsbDriveList.__LogError(null, "UsbPrivate.LoadUsbFromDriveLetter", e); return(null); } return(null); }
public UsbDriveInfo CreateEmptyUsbInfo(string modelName, string volumeName, string uniqueId) { UsbDriveInfo driveInfo = new UsbDriveInfo(modelName, volumeName, uniqueId); mDriveInfoList.Add(driveInfo); return(driveInfo); }
public override bool Equals(object obj) { if (obj != null && obj is UsbDriveInfo) { UsbDriveInfo lhs = (UsbDriveInfo)obj; return(string.Compare(lhs.UniqueID, UniqueID, false) == 0); } else { return(base.Equals(obj)); } }
void WinThread_mDriveDetector_DeviceInserted(object driveLetterObject) { try { char driveLetter = ( char )driveLetterObject; UsbDriveInfo driveInfo = UsbPrivate.LoadUsbFromDriveLetter(driveLetter); if (DeviceInserted != null && driveInfo != null) { DeviceInserted(this, driveInfo); } } catch (System.Exception e) { __LogError(this, "DeviceInserted failed", e); } }
public static UsbDriveInfo BuildFromXml(XmlNode parentNode) { string uniqueId = PWLib.XmlHelp.DirtyString(PWLib.XmlHelp.GetAttribute(parentNode, "uniqueid", "")); string modelName = PWLib.XmlHelp.DirtyString(PWLib.XmlHelp.GetAttribute(parentNode, "modelname", "")); string volumeName = PWLib.XmlHelp.DirtyString(PWLib.XmlHelp.GetAttribute(parentNode, "volumename", "")); // see if usb device is currently plugged in, otherwise return new usbdriveinfo object which is marked inactive UsbDriveInfo foundDriveInfo = UsbDriveList.Instance.GetUsbInfoByUniqueId(uniqueId); if (foundDriveInfo != null) { return(foundDriveInfo); } else { return(UsbDriveList.Instance.CreateEmptyUsbInfo(modelName, volumeName, uniqueId)); } }
void WinThread_mDriveDetector_DeviceRemoved(object driveLetterObject) { try { char driveLetter = (char)driveLetterObject; UsbDriveInfo driveInfo = GetUsbInfoByDriveLetter(driveLetter); if (driveInfo != null) { driveInfo.__MarkInactive(); if (DeviceRemoved != null) { DeviceRemoved(this, driveInfo); } } } catch (Exception e) { __LogError(this, "DeviceRemoved", e); } }
void mDriveDetector_QueryRemove(object sender, char driveLetter, out bool allowRemoval) { allowRemoval = true; try { UsbDriveInfo driveInfo = GetUsbInfoByDriveLetter(driveLetter); if (driveInfo != null) { allowRemoval = !driveInfo.MediaLocked; if (DeviceQueryRemove != null) { DeviceQueryRemove(this, driveInfo, ref allowRemoval); } } } catch (System.Exception e) { __LogError(this, "QueryRemove", e); } }
public static List <UsbDriveInfo> BuildUsbDiskList() { List <UsbDriveInfo> list = new List <UsbDriveInfo>(); try { ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DiskDrive"); foreach (ManagementObject diskObj in searcher.Get()) { if (diskObj["InterfaceType"].ToString() == "USB") { /* Contents of diskObj * * * foreach ( System.Management.PropertyData name in diskObj.Properties ) * { * System.Diagnostics.Debug.WriteLine( name.Name + " : " + ( name.Value != null ? name.Value.ToString() : "" ) ); * } * * Availability : * BytesPerSector : 512 * Capabilities : System.UInt16[] * CapabilityDescriptions : System.String[] * Caption : Samsung S2 Portable USB Device * CompressionMethod : * ConfigManagerErrorCode : 0 * ConfigManagerUserConfig : False * CreationClassName : Win32_DiskDrive * DefaultBlockSize : * Description : Disk drive * DeviceID : \\.\PHYSICALDRIVE2 * ErrorCleared : * ErrorDescription : * ErrorMethodology : * FirmwareRevision : * Index : 2 * InstallDate : * InterfaceType : USB * LastErrorCode : * Manufacturer : (Standard disk drives) * MaxBlockSize : * MaxMediaSize : * MediaLoaded : True * MediaType : External hard disk media * MinBlockSize : * Model : Samsung S2 Portable USB Device * Name : \\.\PHYSICALDRIVE2 * NeedsCleaning : * NumberOfMediaSupported : * Partitions : 1 * PNPDeviceID : USBSTOR\DISK&VEN_SAMSUNG&PROD_S2_PORTABLE&REV_\00000011E09310500A1B&0 * PowerManagementCapabilities : * PowerManagementSupported : * SCSIBus : * SCSILogicalUnit : * SCSIPort : * SCSITargetId : * SectorsPerTrack : 63 * SerialNumber : * Signature : 3392227871 * Size : 1000202273280 * Status : OK * StatusInfo : * SystemCreationClassName : Win32_ComputerSystem * SystemName : SOFTWARE118 * TotalCylinders : 121601 * TotalHeads : 255 * TotalSectors : 1953520065 * TotalTracks : 31008255 * TracksPerCylinder : 255 * */ foreach (ManagementObject partitionObj in diskObj.GetRelated("Win32_DiskPartition")) { /* contents of partitionObj * * Access : * Availability : * BlockSize : 512 * Bootable : True * BootPartition : True * Caption : Disk #2, Partition #0 * ConfigManagerErrorCode : * ConfigManagerUserConfig : * CreationClassName : Win32_DiskPartition * Description : Installable File System * DeviceID : Disk #2, Partition #0 * DiskIndex : 2 * ErrorCleared : * ErrorDescription : * ErrorMethodology : * HiddenSectors : * Index : 0 * InstallDate : * LastErrorCode : * Name : Disk #2, Partition #0 * NumberOfBlocks : 1953520001 * PNPDeviceID : * PowerManagementCapabilities : * PowerManagementSupported : * PrimaryPartition : True * Purpose : * RewritePartition : * Size : 1000202240512 * StartingOffset : 32768 * Status : * StatusInfo : * SystemCreationClassName : Win32_ComputerSystem * SystemName : SOFTWARE118 * Type : Installable File System * */ foreach (ManagementBaseObject logicalObj in partitionObj.GetRelated("Win32_LogicalDisk")) { /* Contents of logicalObj * Access : 0 * Availability : * BlockSize : * Caption : G: * Compressed : False * ConfigManagerErrorCode : * ConfigManagerUserConfig : * CreationClassName : Win32_LogicalDisk * Description : Local Fixed Disk * DeviceID : G: * DriveType : 3 * ErrorCleared : * ErrorDescription : * ErrorMethodology : * FileSystem : NTFS * FreeSpace : 424120320 * InstallDate : * LastErrorCode : * MaximumComponentLength : 255 * MediaType : 12 * Name : G: * NumberOfBlocks : * PNPDeviceID : * PowerManagementCapabilities : * PowerManagementSupported : * ProviderName : * Purpose : * QuotasDisabled : True * QuotasIncomplete : False * QuotasRebuilding : False * Size : 1000202240000 * Status : * StatusInfo : * SupportsDiskQuotas : True * SupportsFileBasedCompression : True * SystemCreationClassName : Win32_ComputerSystem * SystemName : SOFTWARE118 * VolumeDirty : False * VolumeName : PW * VolumeSerialNumber : 34FA652F */ string modelName = diskObj["Model"].ToString(); string volumeName = logicalObj["VolumeName"].ToString(); object serialNumberObject = logicalObj["VolumeSerialNumber"]; object pnpDeviceId = diskObj["PNPDeviceID"]; string uniqueId = FormatDriveUniqueID(serialNumberObject != null ? serialNumberObject.ToString() : "", pnpDeviceId != null ? pnpDeviceId.ToString() : ""); UsbDriveInfo driveInfo = UsbDriveList.Instance.GetUsbInfoByUniqueId(uniqueId); if (driveInfo == null) { driveInfo = UsbDriveList.Instance.CreateEmptyUsbInfo(modelName, volumeName, uniqueId); } string sizeString = logicalObj["Size"].ToString(); System.UInt64 sizeBytes = 0; System.UInt64.TryParse(sizeString, out sizeBytes); driveInfo.__MarkActive(logicalObj["Name"].ToString()[0], sizeBytes); list.Add(driveInfo); } } } } } catch (ManagementException) { } return(list); }