C++ Builder has a lot of specific methods in its SysUtils library which are included in the vcl and fmx libraries. Some of these are grouped in Path Manipulation Routines that allows user 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 FileName property of OpenDialog, SaveDialog components. We can also check drives, files, or directories if they exist or not in that given path. And we can get the home directory path of the operating system.
In this post, you’ll get answers to these questions:
- How do I get Home Path of the System?
- How we can use GetHomePath Method in C++?
- How can I easily find the location of Roaming Directory?
By learning how to use GetHomePath method in C++Builder, it will help you to easily build C++ applications with the use of C++ Software.
Table of Contents
What is the GetHomePath Method?
GetHomePath Method (System:IOUtils:TPath:GetHomePath) is a Path Manipulation Routine that returns either the home path of the user or the application’s writable scratch directory or storage.
We can call GetHomePath() Method to obtain the user’s home path on the supported target platforms. If the system running your application does not support the requested folder, or if the requested folder does not exist in the system, this function returns an empty string instead.
We should use GetHomePath to store settings per user. For example:
1 |
TFile.WriteAllText(TPath.GetHomePath() + TPath.DirectorySeparatorChar + 'sample.txt', 'This is my sample text.'); |
GetHomePath Method points to the following locations on the various platforms:
- On Windows, it points to the user’s application data folder.
- On Linux and OS X, it points to the user’s home folder, as defined by the $(HOME) environment variable.
- On iOS and Android, it points to the device-specific location of the sandbox for the application; the iOS home location is individually defined for each application instance and for each iOS device.
Platform | Sample path | Path ID |
---|---|---|
Windows XP | C:\Documents and Settings\<username>\Application Data | CSIDL_APPDATA |
Windows Vista or later | C:\Users\<username>\AppData\Roaming | FOLDERID_RoamingAppData |
OS X | /Users/<username> | NSUserDirectory |
iOS Device | /private/var/mobile/Containers/Data/Application/<application ID> | |
iOS Simulator | /Users/<username>/Library/Developer/CoreSimulator/Devices/<Device ID>/data/Containers/Data/Application/<application ID> | |
Android | /data/data/<application ID>/files | Context.getFilesDir |
Linux | /home/<username> |
What is the Syntax of the GetHomePath method in modern C++?
Here is the Syntax of GetHomePath() Method,
1 2 3 |
UnicodeString __fastcall GetHomePath(); |
Is there a simple example of using the GetHomePath method in modern C++ ?
GetHomePath() Method returns a UnicodeString, We can get Home Path of the system from a given path string as below,
1 2 3 4 |
String path = TPath::GetHomePath(); ShowMessage(path); |
Is there a full example of how to use the GetHomePath method in modern C++ ?
Here is the full C++ Builder VCL example with the Memo (TMemo) component below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <vcl.h> #include <IOUtils.hpp> #pragma hdrstop #include "Get_Methods_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { String path = TPath::GetHomePath(); ShowMessage(path); } |
Note In addition, we can get Temporary Folder Path by using GetTempPath Method. We can also get all Environment Variables by using the GetEnvironmentVariable Method. These methods will be explained in other posts.
If you are looking to Paths and Directories of C++ Builder IDE Options, they are listed here. C++ Builder Compiler Directories and Conditionals are listed here Environment variables are listed here
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.