Drive Formatter using C#
Posted by Samath
Last Updated: December 30, 2016

This program is used to format a USB Drive or a hard drive.

Code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using Microsoft.VisualBasic;
  7. using Microsoft.VisualBasic.ApplicationServices;
  8. using System.Linq;
  9. using System.IO;
  10. using System.Diagnostics;
  11. using System.Text;
  12. using System.Windows.Forms;
  13.  
  14. namespace Drive_Formatter
  15. {
  16. public partial class Form1 : Form
  17. {
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. }
  22. string request,strName;
  23. Process MyProcess = new Process();
  24. private void button1_Click(object sender, EventArgs e)
  25. {
  26. DriveInfo SelectedDrive = new DriveInfo(comboBoxDrives.Text);
  27. if (MessageBox.Show("Do you really want to format?", "Info", MessageBoxButtons.YesNo) == DialogResult.Yes)
  28. {
  29.  
  30. if (SelectedDrive.DriveType == DriveType.Removable)
  31. {
  32. //check if quick format checkbox is checked
  33. if (checkBoxQuickformat.Checked == false)
  34. {
  35. strName = textBoxVolumeLabel.Text.Trim();
  36. request = "Format /v:" + strName + " /x /backup " + SelectedDrive.Name.Replace("\\", " ");
  37. MyProcess.StartInfo.RedirectStandardOutput = true;
  38. MyProcess.StartInfo.RedirectStandardError = true;
  39. MyProcess.StartInfo.CreateNoWindow = true;
  40. MyProcess.StartInfo.UseShellExecute = false;
  41. //this try statement is here to avoid the application to crash,
  42. //when the is no enough space, read-only location
  43. try
  44. {
  45. File.WriteAllText("batman.bat", request + Environment.NewLine);
  46. }
  47. catch (IOException m)
  48. {
  49. MessageBox.Show(m.Message.ToString());
  50. buttonFormat.Enabled = true;
  51. return;
  52. }
  53. MyProcess.StartInfo.FileName = "batman.bat";
  54. //START the process
  55. MyProcess.Start();
  56. timer1.Enabled = true;
  57.  
  58. }
  59. else
  60. {
  61. strName = textBoxVolumeLabel.Text.Trim();
  62. //note the below line can also work on Limited/Standard user
  63. request = "Format /v:" + strName + " /x /Q /backup " + SelectedDrive.Name.Replace("\\", " ");
  64. MyProcess.StartInfo.RedirectStandardOutput = true;
  65. MyProcess.StartInfo.RedirectStandardError = true;
  66. MyProcess.StartInfo.CreateNoWindow = true;
  67. MyProcess.StartInfo.UseShellExecute = false;
  68. //this try statement is here to avoid the application to crash,
  69. //when the is no enough space, read-only location
  70. try
  71. {
  72. File.WriteAllText("batman.bat", request + Environment.NewLine);
  73. }
  74. catch (IOException m)
  75. {
  76. MessageBox.Show(m.Message.ToString());
  77. buttonFormat.Enabled = true;
  78. return;
  79. }
  80. MyProcess.StartInfo.FileName = "batman.bat";
  81. //START the process
  82. MyProcess.Start();
  83. timer1.Enabled = true;
  84. }
  85. }
  86. else if (SelectedDrive.DriveType == DriveType.Fixed)
  87. {
  88. if (checkBoxQuickformat.Checked == false)
  89. {
  90. MyProcess.StartInfo.RedirectStandardOutput = true;
  91. MyProcess.StartInfo.RedirectStandardError = true;
  92. MyProcess.StartInfo.CreateNoWindow = true;
  93. MyProcess.StartInfo.UseShellExecute = false;
  94. try
  95. {
  96. File.WriteAllText("batman.bat", "Format /x /v:" + textBoxVolumeLabel.Text + " " + comboBoxDrives.SelectedItem.ToString());
  97. }
  98. catch (IOException m)
  99. {
  100. MessageBox.Show(m.Message.ToString());
  101. buttonFormat.Enabled = true;
  102. return;
  103. }
  104. MyProcess.StartInfo.FileName = "batman.bat";
  105. //START the process
  106. MyProcess.Start();
  107. timer1.Enabled = true;
  108. }
  109. else
  110. {
  111. MyProcess.StartInfo.RedirectStandardOutput = true;
  112. MyProcess.StartInfo.RedirectStandardError = true;
  113. MyProcess.StartInfo.CreateNoWindow = true;
  114. MyProcess.StartInfo.UseShellExecute = false;
  115. try
  116. {
  117. File.WriteAllText("batman.bat", "Format /q /x /v:" + textBoxVolumeLabel.Text + " " + comboBoxDrives.SelectedItem.ToString());
  118. }
  119. catch (IOException m)
  120. {
  121. MessageBox.Show(m.Message.ToString());
  122. buttonFormat.Enabled = true;
  123. return;
  124. }
  125. MyProcess.StartInfo.FileName = "batman.bat";
  126. //START the process
  127. MyProcess.Start();
  128. timer1.Enabled = true;
  129. }
  130.  
  131.  
  132.  
  133. }
  134.  
  135.  
  136. }
  137. }
  138. bool isFormating=false ;
  139. private void timer1_Tick(object sender, EventArgs e)
  140. {
  141. if (labelProgress.Text.Contains("Volume Serial") == false)
  142. {
  143. //check if format was aborted
  144. if (labelProgress.Text.Contains("Format aborted.") == false)
  145. {
  146. labelInfo.Text = "Please wait, there formater is busy...";
  147. //get the current progress
  148.  
  149. labelProgress.Text = MyProcess.StandardOutput.ReadLine().ToString();
  150. //disable the format button
  151. buttonFormat.Enabled = false;
  152. isFormating = true;
  153. }
  154. else {
  155. timer1.Enabled = false;
  156. labelInfo.Text = "Format aborted.";
  157. MessageBox.Show(labelInfo.Text, "Error");
  158. labelProgress.Text = string.Empty;
  159. isFormating = false;
  160. buttonFormat.Enabled = true;
  161. }
  162. if (labelProgress.Text.Contains("Enter current volume label") == true )
  163. {
  164. StreamWriter stremwriter ;
  165. stremwriter = MyProcess.StandardInput;
  166. stremwriter.Write(FixedHardDriveName);
  167. stremwriter.Close();
  168. }
  169.  
  170. }
  171. else
  172. {
  173. timer1.Enabled = false;
  174. labelInfo.Text = "Done Formating";
  175. labelProgress.Text = string.Empty;
  176. MessageBox.Show(labelInfo.Text);
  177. isFormating = false;
  178. buttonFormat.Enabled = true;
  179.  
  180. }
  181. }
  182.  
  183. private void textBoxVolumeLabel_KeyPress(object sender, KeyPressEventArgs e)
  184. {
  185. //allow only alphabets to be entered in the textbox
  186. if (((e.KeyChar < 'a') || (e.KeyChar > 'z')) && ((e.KeyChar < 'A') || (e.KeyChar > 'Z' )))
  187. {
  188. e.Handled = true;
  189. }
  190. }
  191. Microsoft.VisualBasic.ApplicationServices.User Myuser;
  192. private void Form1_Load(object sender, EventArgs e)
  193. {
  194. Myuser = new User();
  195. if (Myuser.IsInRole("Administrators") != true)
  196. {
  197. //get all attached removable drives
  198. foreach (string AttachedDrive in Environment.GetLogicalDrives())
  199. {
  200.  
  201. DriveInfo CompDrive = new DriveInfo(AttachedDrive);
  202.  
  203. if ((CompDrive.Name != "A:\\") && (CompDrive.Name != "B:\\"))
  204. {
  205. //allow only removable and fixed drives to be added in the combobox
  206. if ((CompDrive.DriveType == DriveType.Removable) || (CompDrive.DriveType == DriveType.Fixed))
  207. {
  208.  
  209. //add drive name into the combobox
  210. comboBoxDrives.Items.Add(CompDrive.Name);
  211.  
  212. }
  213. }
  214. }
  215. //select the first item on the combobox
  216. comboBoxDrives.SelectedIndex = 0;
  217.  
  218. }
  219. else
  220. {
  221. MessageBox.Show("Administrators prevelegies required.");
  222. this.Close();
  223. }
  224. }
  225. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  226. {
  227. if (isFormating == true)
  228. {
  229. e.Cancel = false;
  230. }
  231. else
  232. {
  233. if (File.Exists("batman.bat") == true)
  234. {
  235. //delete the batman.bat file
  236. File.Delete("batman.bat");
  237. }
  238. }
  239. //check if the application is closed by a taskmanager
  240. if (e.CloseReason == CloseReason.TaskManagerClosing)
  241. {
  242. if (File.Exists("batman.bat") == true)
  243. {
  244. //delete the batman.bat file
  245. File.Delete("batman.bat");
  246. }
  247. }
  248. }
  249.  
  250. private void buttonClose_Click(object sender, EventArgs e)
  251. {
  252. if (isFormating == false)
  253. {
  254. if (File.Exists("batman.bat") == true)
  255. {
  256. //delete the batman.bat file
  257. File.Delete("batman.bat");
  258. this.Close();
  259. }
  260. }
  261. }
  262. string FixedHardDriveName;
  263. private void comboBoxDrives_SelectedValueChanged(object sender, EventArgs e)
  264. {
  265. DriveInfo SelectedDrive = new DriveInfo(comboBoxDrives.Text);
  266. if (SelectedDrive.IsReady == true )
  267. {
  268. if (SelectedDrive.VolumeLabel == String.Empty )
  269. {
  270. textBoxVolumeLabel.Text = "Removable Disc";
  271. FixedHardDriveName = SelectedDrive.VolumeLabel;}
  272. else{
  273. FixedHardDriveName = SelectedDrive.VolumeLabel;
  274. textBoxVolumeLabel.Text = SelectedDrive.VolumeLabel;
  275. }
  276.  
  277. }
  278.  
  279. }
  280. }
  281. }