❔ Crossplatform disk formatting code
I am looking for a cross-platform way of formatting a disk. Could anyone guide me through it or give me some references? Thank you in advance : )
12 Replies
Disks operate pretty differently per system, are you really trying to reformat them? Or maybe just empty them? Is there a specific format you want?
Basically, what I am gonna do is that.. format and partition them afterwards
So, I guess for doing partitioning, formatting is necessary.
Mmh.
Here’s a windows version:
https://www.codeproject.com/Articles/115598/Formatting-a-Drive-using-C-and-WMI
But this actually uses its code to DO the formatting, where yours is using an external utility to format.
I would probably stick with your current approach and learn more about how to execute what you want fron the commandline using DiskUtil for Mac and CMD for windows.
Yeah, I guess that is better.. let me see how I can utilize my own code.
Tbh I’d rely on whatever the system is providing for disk formatting.
Why touch that if you don’t have to?
Here’s a mac version:
https://howchoo.com/apple/format-hard-drive-macos
Howchoo
How to Format a Hard Drive on macOS
This guide covers how to format a hard drive using macOS. These processes will work for both internal and external hard drives—even USB flash drives.
I think your biggest problem is actually just going to be figuring out what drive to format.
MacOS and linux use a naming scheme that is dramatically different fron Windows’ drive letters.
Yeah, I already got the mac command from this url
yeah.. but system.IO is cross-platform right?
It is, but, where’s the string “disk” come from?
Like, if you gave it the string “C:” a mac is not gonna have a clue what that is regardless.
If the full proper name was provided,
“/dev/disk1” it’d try to format “/dev/dev/disk1” and bust
If you happen to be shooting for linux compatibility as well, disk handling works the same way there as macOS, just not using diskutil.
Yeah got it
thank you
That's why you build path's not with String literals / Magic Strings. Instead you can for example use the
Environment.SpecialFolder
enum to build your path.
Example given Environtment.SpecialFolder.ApplicationData
resolves to:
Windows: %APPDATA%
, which is typically C:\Users\{username}\AppData\Roaming
Mac: ~/Library/Application Support
Linux: ~/.config
there is a whole list of these specialfolders
using Path.Combine()
we can achieve different seperators independant of the platform:
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.