FileExists (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following code prompts for confirmation before deleting a file. Create a file and then click the button to select the file with a TOpenDialog. Click Yes to delete it.

Code

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  String FullFileName = Edit1->Text;
  String FileName = ExtractFileName(FullFileName);
  if (FileExists(FullFileName)) {
    TDialogService::MessageDialog(
              Format("Do you want to delete '%s'?", ARRAYOFCONST((FileName))),
              TMsgDlgType::mtConfirmation,
              mbYesNo,
              TMsgDlgBtn::mbNo, 0,
              [FullFileName](const System::Uitypes::TModalResult AResult)
              {
                if (AResult == mrYes)
                  DeleteFile(FullFileName);
              });
  } else {
    TDialogService::ShowMessage(Format("File '%s' does not exist.",
                                       ARRAYOFCONST((FullFileName))));
  }
}

Uses