AndyD273
AndyD273

Reputation: 7279

Get HDD Serial Number in Swift

I'm starting a program that needs to keep track of external hard drives.
I'm looking for the best way to uniquely identify a hard drive across several computers (including windows computers with .Net down the road).

I looked at getting the UUID, but that appears to be different across computers. I was hoping to try to get the manufacturer serial number that's printed on the drive, but I don't know how to do it with Swift, if it's even possible.

Extra bonus if I could also tie this into detecting when a new HDD is mounted, but not a DVD.

Upvotes: 0

Views: 799

Answers (1)

Mundi
Mundi

Reputation: 80265

Note that NSFileManager has this method

mountedVolumeURLsIncludingResourceValuesForKeys(_:options:)

and under options you can specify NSURLVolumeIdentifierKey. This is a temporary identifier (not consistent across restarts) but it is unique and you would be able to monitor newly added or ejected volumes this way.

I am sure there is also another key to tell you about the type of file system and other attributes that let you determine if it is a DVD.

Follow the link to Common File System Resource Keys from the documentation of above method.

Maybe something like

let attributes = try manager.attributesOfFileSystemForPath("/")

could also be of help. In particular, NSFileSystemNumber seems to be a unique number as well.

Upvotes: 2

Related Questions