C
C#16mo ago
Abdesol

❔ 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
magicaldave
magicaldave16mo ago
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?
Abdesol
Abdesol16mo ago
Basically, what I am gonna do is that.. format and partition them afterwards So, I guess for doing partitioning, formatting is necessary.
magicaldave
magicaldave16mo ago
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.
CodeProject
Formatting a Drive using C# and WMI
Formatting a drive using C# and WMI
magicaldave
magicaldave16mo ago
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.
Abdesol
Abdesol16mo ago
Yeah, I guess that is better.. let me see how I can utilize my own code.
magicaldave
magicaldave16mo ago
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.
magicaldave
magicaldave16mo ago
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.
Abdesol
Abdesol16mo ago
Yeah, I already got the mac command from this url yeah.. but system.IO is cross-platform right?
magicaldave
magicaldave16mo ago
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.
Abdesol
Abdesol16mo ago
Yeah got it thank you
Florian Voß
Florian Voß16mo ago
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:
string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string dataFilePath = Path.Combine(appDataFolder, "MyApp", "data.txt");
string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string dataFilePath = Path.Combine(appDataFolder, "MyApp", "data.txt");
Accord
Accord16mo ago
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.