Add two TButton components to the form, one to create a new folder, the other to delete the newly created folder, and the design-complete form as shown in Figure 1.
Figure 1 Main interface
You first add a reference to Shellapi in the uses section of your program, and add the following code to the response of the Create button:
Procedure Tform1.btncreateclick (Sender:tobject);
Begin
CreateDirectory (Pchar (Extractfilepath (paramstr (0)) + ' Test '), nil);
End
The program executes the CreateDirectory (Pchar (Extractfilepath (0) + ' Test '), nil) statement at the beginning of its operation, creating a subdirectory Test in the directory where the program instance resides.
When the program is running, click the Delete button to delete the newly created test subdirectory with the following response code:
Procedure Tform1.btndeleteclick (Sender:tobject);
Var
T:tshfileopstruct;
p:string;
Begin
P:=extractfilepath (paramstr (0)) + ' Test ';
With T do
Begin
wnd:=0;
Wfunc:=fo_delete;
Pfrom:=pchar (P);
Pto:=nil;
Fflags:=fof_allowundo+fof_noerrorui;
Hnamemappings:=nil;
lpszprogresstitle:= ' deleting folder ';
Fanyoperationsaborted:=false;
End
SHFileOperation (T);
End
The program can undo the deletion by populating the properties of the Tshfileopstruct type Object T, and of course the final deletion is done through SHFileOperation (t). If the SHFileOperation function returns a value of 0, it indicates that the deletion succeeded or that the deletion failed.
If the user wants to delete the Test subdirectory directly in the program, you can do so directly by calling the RemoveDirectory (Extractfilepath (paramstr (0)) + ' Test ') statement
The program code is as follows:
Unit Unit1;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls, Shellapi;
Type
TForm1 = Class (Tform)
Btncreate:tbutton;
Btndelete:tbutton;
Procedure Btncreateclick (Sender:tobject);
Procedure Btndeleteclick (Sender:tobject);
Private
{Private declarations}
Public
{Public declarations}
End
Var
Form1:tform1;
Implementation
{$R *.DFM}
Procedure Tform1.btncreateclick (Sender:tobject);
Begin
CreateDirectory (Pchar (Extractfilepath (paramstr (0)) + ' Test '), nil);
End
Procedure Tform1.btndeleteclick (Sender:tobject);
Var
T:tshfileopstruct;
p:string;
Begin
P:=extractfilepath (paramstr (0)) + ' Test ';
With T do
Begin
wnd:=0;
Wfunc:=fo_delete;
Pfrom:=pchar (P);
Pto:=nil;
Fflags:=fof_allowundo+fof_noerrorui;
Hnamemappings:=nil;
lpszprogresstitle:= ' deleting folder ';
Fanyoperationsaborted:=false;
End
SHFileOperation (T);
RemoveDirectory (Extractfilepath (paramstr (0)) + ' Test ') (Pchar);
End
End.
Save the file, and then press F9 to run the program, the initial picture of the program running as shown in Figure 2.
Figure 2 Initial screen of program operation
Clicking the Create button creates a subdirectory test in the same directory as the program instance, and you can delete the newly created test subdirectory by clicking the Delete button.
This example describes how to create a folder and provides two ways to delete a folder-one is to delete the folder to the Recycle Bin, and the other is to delete it directly.