This example describes how to create a folder in a program, and you can delete a newly created folder to the Recycle Bin.
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:=’正在删除文件夹’;
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, it can be done directly by calling the RemoveDirectory (Extractfilepath (paramstr (0)) + ' Test ') statement.