C
C#2y 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
magicaldave2y 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
AbdesolOP2y ago
Basically, what I am gonna do is that.. format and partition them afterwards So, I guess for doing partitioning, formatting is necessary.
magicaldave
magicaldave2y 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
magicaldave2y 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
AbdesolOP2y ago
Yeah, I guess that is better.. let me see how I can utilize my own code.
magicaldave
magicaldave2y 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
magicaldave2y 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
AbdesolOP2y ago
Yeah, I already got the mac command from this url yeah.. but system.IO is cross-platform right?
magicaldave
magicaldave2y 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
AbdesolOP2y ago
Yeah got it thank you
Florian Voß
Florian Voß2y 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
Accord2y 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.
Want results from more Discord servers?
Add your server