format a drive in csharp

To format a drive in C#, you can use the DriveInfo class and the Format method from the System.IO namespace.

Here's an example code block that shows how to format a drive:

main.cs
using System.IO;

// specify the drive letter of the drive you want to format (e.g., "C:")
string driveLetter = "D:";

// create a DriveInfo object for the specified drive
DriveInfo drive = new DriveInfo(driveLetter);

// check if the drive is ready to be formatted
if (drive.IsReady)
{
   // format the drive using NTFS file system
   drive.Format("NTFS", quickFormat: true);
}
else
{
   Console.WriteLine($"Drive '{driveLetter}' is not ready to be formatted.");
}
466 chars
19 lines

Note: Formatting a drive will erase all data on the drive, so be careful when using this method.

gistlibby LogSnag