C++ Builder has a lot of specific methods in its SysUtils library that are included in the vcl and fmx libraries. Some of these are grouped as Path Manipulation Routines that allow users to edit, extract, get and set drive name, directory name, file name, and file extensions. These methods are combined in Vcl.FileCtrl, System.IOUtils, System.SysUtils libraries. These all methods are easy to use and easy to get or set file path strings in that operating system. These can be used with other component properties like the FileName property of OpenDialog, SaveDialog components. We can also check drives, files, or directories to see if they exist or not on a given path. We can also get some specific directory locations like “Current Directory”.
In tIn this post, you’ll learn what the term “current directory” means, how to get the current directory, how to use the GetCurrentDir method, and how to use the GetCurrentDirectory method in C++ software. By learning how to use GetCurrentDir and SetCurrentDir methods to get or to set Current Directory on Windows, it will help you to easily build C++ applications.
Table of Contents
What are the C++ GetCurrentDir and SetCurrentDir methods?
Current Directory term is used for the active directory that our executable application is in it. On Windows, when your application runs, this Current Directory is the directory where your running application is. That means if you try to open a file this file should be in that folder or if you want to write a file this file will be created in that Current Directory. If you want to work on other directory you can use full path or you can change Current Directory by using SetCurrentDir() method.
GetCurrentDir Method (System::SysUtils::GetCurrentDir) is a SysUtils Method that returns the name of the current directory. It returns the fully qualified name as in String (UnicodeString) of the current directory.
SetCurrentDir Method (System::SysUtils::SetCurrentDir) is a SysUtils Method that sets the current directory. The return value is True if the current directory was successfully changed, or False if an error occurred.
What is the syntax of the C++ GetCurrentDir and SetCurrentDir methods?
Here is the Syntax of GetCurrentDir Method:
1 2 3 |
System::UnicodeString __fastcall GetCurrentDir(void); |
Here is the Syntax of SetCurrentDir() Method:
1 2 3 |
bool __fastcall SetCurrentDir(const System::UnicodeString Dir); |
A simple example of using the C++ GetCurrentDirectory and SetCurrentDirectory methods
GetCurrentDir Method returns a UnicodeString, we can check a file and set the result to a bool as below,
1 2 3 4 |
String cd = GetCurrentDir(); ShowMessage(cd); |
We can use SetCurrentDir to set current directory to a path as in this example below
1 2 3 4 5 |
SetCurrentDir( L"C:\\Users\\Public"); String cd = GetCurrentDir(); ShowMessage(cd); |
Here is a full example of how to use the C++ GetCurrentDir and SetCurrentDir methods
Here is the example how we check if a directory exists or not,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <fmx.h> #pragma hdrstop #include "Get_Current_Direcorty_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.fmx" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { String cd; // UnicodeString cd = GetCurrentDir(); ShowMessage(cd); SetCurrentDir( L"C:\\Users\\Public"); cd = GetCurrentDir(); ShowMessage(cd); } |
This is an example which uses the C++ GetCurrentDirectory and SetCurrentDirrectory methods
Note that there are GetCurrentDirectory (System.IOUtils.TDirectory.GetCurrentDirectory) and SetCurrentDirectory (System.IOUtils.TDirectory.SetCurrentDirectory) methods used as same above. Note that these methods doesn’t support UnicodeStrings. You must #include <IOUtils.hpp> and you must use TDirectory class name before these methods. We recommend you to use GetCurrentDir and SetCurrentDir methods which supports UnicodeStrings.
Here is a full C++ Builder example that uses both kinds,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <fmx.h> #include <IOUtils.hpp> #pragma hdrstop #include "Get_Current_Direcorty_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.fmx" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { String cd; // UnicodeString cd = GetCurrentDir(); ShowMessage(cd); SetCurrentDir( L"C:\\Users\\Public"); cd = GetCurrentDir(); ShowMessage(cd); cd = TDirectory::GetCurrentDirectory(); ShowMessage(cd); TDirectory::SetCurrentDirectory("C:\\Users\\Public"); cd = GetCurrentDir(); ShowMessage(cd); cd = TDirectory::GetDirectoryRoot( GetCurrentDir()); ShowMessage(cd); } |
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition