Home  |  Delphi .net Home  |  System.IO.Directory  |  Delete Method
Delete  
Method  
Delete a Directory (folder) or nest of Directories
Directory Class
System.IO NameSpace
CF1.  Procedure Delete ( PathSting : String; ) ;
CF2.  Procedure Delete ( PathSting:StringPathSting : String; Recursive : Boolean; ) ; Static;
CF : Methods with this mark are Compact Framework Compatible
Description
If the specified PathString Directory exists, it is deleted. The delete will fail if the directory is not empty, unless the Recursive parameter is true, when all sub-directories are deleted first.
 
The PathString may be absolute or relative to the current directory.
Notes
An exception is thrown if the delete is not recursive and sub-directories are found.

Static methods are not methods of an object - they are simply class functions or procedures available at any time.
Microsoft MSDN Links
System.IO
System.IO.Directory
 
 
Creating and deleting a nest of folders
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  Path : String;

begin
  Path := 'C:\Dir1\Dir2';

  // Create a nest of 2 new folders
  System.IO.Directory.CreateDirectory(Path);

  if System.IO.Directory.Exists(Path)
  then Console.WriteLine('{0} exists', Path)
  else Console.WriteLine('{0} does not exist', Path);

  // Now delete this nest of folders
  System.IO.Directory.Delete('C:\Dir1', true);

  if System.IO.Directory.Exists(Path)
  then Console.WriteLine('{0} exists', Path)
  else Console.WriteLine('{0} does not exist', Path);

  Path := 'C:\Dir1';

  if System.IO.Directory.Exists(Path)
  then Console.WriteLine('{0} exists', Path)
  else Console.WriteLine('{0} does not exist', Path);

  Console.Readline;
end.
Show full unit code
  C:\Dir1\Dir2 exists
  C:\Dir1\Dir2 does not exist
  C:\Dir1 does not exist
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author