RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
System.ChDir Function

Changes the current directory.

Pascal
procedure ChDir(const S: string); overload;
procedure ChDir(P: PChar); overload;
C++
ChDir(const AnsiString S);
ChDir(const char * P);

Note: ChDir changes the current directory to the path specified by S or P. If this operation fails, EInOutError is raised.
Note: On Windows, the path can include a drive specifier, which causes the current drive to be changed as well.
Note: In Delphi, {$I+} handles runtime errors using exceptions. When using {$I-}, use IOResult to check for an I/O error.
 

C++ Examples: 

 

/*
This example shows how to change and display the current
directory through text.  This example requires that a drive
combo box, a file list box, and a directory list box are on
a form. Add this code as the OnChange event handler for the
drive combo box and the OnChange event handler for the
directory list box.  When the user changes the drive using
the combo box, the directory list box and file list box will
update to reflect the new drive and the current directory on
that drive.  When the user double clicks on a directory in
the file list box will update to reflect the new directory.
*/
void __fastcall TMyChDir::Button1Click(TObject *Sender)
{
  try
  {
    System::ChDir(Edit1->Text);
  }
  catch (...)
  {
    MessageDlg("Cannot find directory", mtWarning, TMsgDlgButtons() << mbOK, 0);
  }
  Edit2->Text = GetCurrentDir();
  FileListBox1->Directory = GetCurrentDir();
  DirectoryListBox1->Directory = GetCurrentDir();
}

void __fastcall TMyChDir::DirectoryListBox1Change(TObject *Sender)
{
  FileListBox1->Directory = DirectoryListBox1->Directory;
  Edit2->Text = GetCurrentDir();
}

void __fastcall TMyChDir::DriveComboBox1Change(TObject *Sender)
{
  DirectoryListBox1->Drive = DriveComboBox1->Drive;
  FileListBox1->Drive = DriveComboBox1->Drive;
  FileListBox1->Directory = DirectoryListBox1->Directory;
  Edit2->Text = GetCurrentDir();
}

 

Delphi Examples: 

{
This example shows how to change and display the current
directory through text.  This example requires that a drive
combo box, a file list box, and a directory list box are on
a form. Add this code as the OnChange event handler for the
drive combo box and the OnChange event handler for the
directory list box.  When the user changes the drive using
the combo box, the directory list box and file list box will
update to reflect the new drive and the current directory on
that drive.  When the user double clicks on a directory in
the file list box will update to reflect the new directory.
}
procedure TForm1.Button1Click(Sender: TObject);
begin
  System.ChDir(Edit1.Text);
  if IOResult <> 0 then
    MessageDlg('Cannot find directory', mtWarning, [mbOk], 0);
  Edit2.Text := GetCurrentDir;
  FileListBox1.Directory := GetCurrentDir;
  DirectoryListBox1.Directory := GetCurrentDir;
end;

procedure TForm1.DirectoryListBox1Change(Sender: TObject);
begin
  FileListBox1.Directory := DirectoryListBox1.Directory;
  Edit2.Text := GetCurrentDir;
end;

procedure TForm1.DriveComboBox1Change(Sender: TObject);
begin
  DirectoryListBox1.Drive := DriveComboBox1.Drive;
  FileListBox1.Drive := DriveComboBox1.Drive;
  FileListBox1.Directory := DirectoryListBox1.Directory;
  Edit2.Text := GetCurrentDir;
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!