Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete all files in hard disk

Status
Not open for further replies.

ahm123

Programmer
May 6, 2012
7
SA
can any one give me code to delete all files in hard disk by c#
 
Is that a partitioned disk? Do you want to wipe all the files in a partition or wipe all the partitions?

There are system commands like format and fdisk which do that.
 
I want to delete all file in directory meaning if i have drive D can i try to delete all files found in this drive by c# code
please help me
 
A drive and directory are two different things. A drive is all folders and files where a directory is a subset of the drive. You need to be VERY clear and understand what it is you want to do. formatting the drive will remove ALL files and folders on the drive.

Here is a wiki link to what format is

With that said, here is some code TO GET YOUR STARTEDs. I have not tested this code, since I do not have any drives that I want to format so you will need to review it and test it and make any changes neccessary, like throwing in a Warning to the user explaining to them that this will do:

Code:
static void FormatDrive( System.IO.DriveInfo drive)
{
  if (drive.IsReady)
  {
   System.Diagnostics.Process format = new System.Diagnostics.Process();
   System.Diagnostics.ProcessStartInfo psiFormat = new System.Diagnostics.ProcessStartInfo();
   psiFormat.FileName = "format.exe";
   psiFormat.WorkingDirectory = "%windir%\\systm32";
   psiFormat.Arguments = drive.Name;
   format.StartInfo = psiFormat;
   format.Start();
   psiFormat = null;
   format = null;
  }
}

if you want to just delete files and folders in a directory do this
Code:
new System.IO.DirectoryInfo("c:\\temp\\").Delete(true);
 
A drive and directory are two different things. A drive is all folders and files where a directory is a subset of the drive. You need to be VERY clear and understand what it is you want to do. formatting the drive will remove ALL files and folders on the drive.

Here is a wiki link to what format is

With that said, here is some code TO GET YOUR STARTEDs. I have not tested this code, since I do not have any drives that I want to format so you will need to review it and test it and make any changes neccessary, like throwing in a Warning to the user explaining to them that this will do:

Code:
static void FormatDrive( System.IO.DriveInfo drive)
{
  if (drive.IsReady)
  {
   System.Diagnostics.Process format = new System.Diagnostics.Process();
   System.Diagnostics.ProcessStartInfo psiFormat = new System.Diagnostics.ProcessStartInfo();
   psiFormat.FileName = "format.exe";
   psiFormat.WorkingDirectory = "%windir%\\systm32";
   psiFormat.Arguments = drive.Name;
   format.StartInfo = psiFormat;
   format.Start();
   psiFormat = null;
   format = null;
  }
}

if you want to just delete files and folders in a directory do this
Code:
new System.IO.DirectoryInfo("c:\\temp\\").Delete(true);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top