Click here to Skip to main content
15,894,106 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
How can I forcefully format an usb drive?
Posted
Comments
Henry Minute 12-Nov-10 9:22am    
This has the possibility to be for malicious purposes. So perhaps if you explain why it is that you want to do this and what problems you are currently having, more people would be willing to help you. :)
Toli Cuturicu 12-Nov-10 11:05am    
Even if you can, DON'T!
BillW33 16-Nov-10 10:10am    
Some folk may want to format their USB drive with NTFS, which has advantages and disadvantages. So there can be legit reasons for doing a reformat.

1 solution

public bool Format_USB(string l_usbpath)
{
bool l_usbflag = false;

if (l_usbpath.Length > 0)
{
// once the format is done.
StreamWriter l_sw;
string l_fstype = "ntfs";
string l_vol_label = "Nitin";

l_sw = File.CreateText(@"usb.bat");
l_sw.WriteLine("format " + l_usbpath + " /fs:" + l_fstype + " /v:" + l_vol_label + " /Q");
//l_sw.WriteLine(l_vol_label);
l_sw.Close();

System.Diagnostics.Process l_proc = new System.Diagnostics.Process();
l_proc.StartInfo.FileName = @"usb.bat";
l_proc.StartInfo.UseShellExecute = false;
l_proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
l_proc.Start();
l_proc.WaitForExit();
File.Delete(@"usb.bat");

string[] l_files = Directory.GetFiles(l_usbpath);
string[] l_dir = Directory.GetDirectories(l_usbpath);
if ((l_files.Length == 0) && (l_dir.Length == 0))
{
l_usbflag = true;
}
else
{
l_usbflag = false;
}
}
return l_usbflag;
}


Try this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900