C++Builder How To Use C++ To Get And Set Current Directory On Windows

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
How To Use C++ To Get And Set Current Directory On Windows
By Yilmaz Yoru September 2, 2021

What does “current directory” mean? In C++ Builder, how can we can get current directory? How can I use GetCurrentDir method in C++ Builder? How can I use the GetCurrentDirectory Method in C++ Builder? Let’s answer your questions.

C++ Builder has a lot of specific methods in its Для просмотра ссылки Войди или Зарегистрируйся library that is included in vcl and fmx libraries. Some of these are grouped as a Для просмотра ссылки Войди или Зарегистрируйся that allows user to edit, extract, get and set drive name, directory name, file name and file extensions. These methods are combined in Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйся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 FileName property of OpenDialog, SaveDialog components. We can also check drives, files or directories to see if they are exist or not on a given path. We can also get some specific directory locations like “Current Directory”.

Let’s see how we can use GetCurrentDir and SetCurrentDir Methods to get or to set Current Directory on Windows.

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.

Для просмотра ссылки Войди или Зарегистрируйся 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.

Для просмотра ссылки Войди или Зарегистрируйся 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:
C++:
System::UnicodeString __fastcall GetCurrentDir(void);
Here is the Syntax of SetCurrentDir() Method:
C++:
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,
C++:
String cd = GetCurrentDir();
ShowMessage(cd);
We can use SetCurrentDir to set current directory to a path as in this example below
C++:
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,
C++:
#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 Для просмотра ссылки Войди или Зарегистрируйсяy (System.IOUtils.TDirectory.GetCurrentDirectory) and Для просмотра ссылки Войди или Зарегистрируйся (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,
C++:
#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);
}