Delphi Programming Create and delete folders

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.