Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read a "drive label" or "volume name" in .NET?

Tags:

.net

wmi

Can you show sample code for reading a drive label or volume name in .NET? I get the sense this requires WMI, but I am loathe to "drop-down" into WMI because it is like dropping down into a string-based SQL query in the sense that certain objects may not exist on certain versions of OSes or the user may not have the right to query certain data. I will be happy to be convinced that I'm wrong about WMI ...

like image 855
flipdoubt Avatar asked Dec 06 '22 07:12

flipdoubt


2 Answers

No WMI required. The following will get all volume labels:

var labels = from drive in DriveInfo.GetDrives()
             select drive.VolumeLabel
like image 109
Kent Boogaart Avatar answered Dec 28 '22 23:12

Kent Boogaart


Call DriveInfo.GetDrives to get an array of drive information. Then look at DriveInfo.VolumeLabel

like image 31
Sean Avatar answered Dec 29 '22 01:12

Sean