dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.96k stars 4.65k forks source link

System.IO.FileSystem.DriveInfo: Support SSD and HDD for DriveType #98675

Open htcfreek opened 6 months ago

htcfreek commented 6 months ago

The DriveType property of System.IO.FileSyste.DriveInfo currently does support CD and Removable. But for internal drives it always says fixed. It can not decide between SSD and HDD.

Implementation suggestion

A new property that not brakes the existing one called PhysicalMedia with the following supported values:

ghost commented 6 months ago

Tagging subscribers to this area: @dotnet/area-system-io See info in area-owners.md if you want to be subscribed.

Issue Details
The `DriveType` property of `System.IO.FileSyste.DriveInfo` currently does support `CD` and `Removable`. But for internal drives it always says `fixed`. It can not decide between SSD and HDD. #Implementation suggestion A new property `PhysicalMedia` with the following supported values: - `Unknown` - `CD-Rom` or `DVD-Rom` or similar one. - `Floppy` - `SSD` - `HDD` - `Network Share` - `Virtual` or `Raid` (And maybe more.)
Author: htcfreek
Assignees: -
Labels: `area-System.IO`, `untriaged`
Milestone: -
hopperpl commented 6 months ago

Retrieving the physical type of a drive is highly complicated because you cross the boundary between a logical volume and a physical device. A logical device can span multiple physical devices.

In windows, DeviceIoControl() with IOCTL_STORAGE_QUERY_PROPERTY returns the information for property id StorageDeviceSeekPenaltyProperty in DEVICE_SEEK_PENALTY_DESCRIPTOR::IncursSeekPenalty which might be the closest to SSD vs HDD. But you need the correct volume handle.

I'm not sure if this information fits into the runtime because of all the edge cases to consider. In general, DriveInfo already is not reliable because it doesn't handle most multi-span or non-drive-letter volumes correctly. In a nutshell, any directory can be a volume mount point. Which means, my volume C:\ can be an SSD, but I can also mount a volume at C:\External\Drive-123 to an HDD. It would just return wrong results. If you use virtual volumes that combine multiple other volumes (like stacking), then every directory can reside on a different volume. There are so many edge cases to consider.

Personally, I don't think it's worth it. This goes too deep into system information and would require a specific library that is designed to handle all this. Any user would expect a 100% correct result and not a "95% right but can be wrong". Especially if you try to optimize of an SSD drive and the runtime says it's an SSD when it's actually an HDD.