Skip to content

Commit 65deee2

Browse files
committed
Update 5.md
1 parent c01e7a2 commit 65deee2

File tree

1 file changed

+101
-6
lines changed
  • docs/documentation/tips_and_tricks

1 file changed

+101
-6
lines changed

docs/documentation/tips_and_tricks/5.md

Lines changed: 101 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Getting special folders (🟢 Beginner)
22

3-
How to get special folders such : Home, Desktop, Program Files, ...
3+
Quickly get the paths to your user’s Desktop, Home, or Program Files folder—cross-platform, in just a few lines of code!
44

55
## Win32
66

@@ -30,7 +30,7 @@ std::string get_known_folder_path(int id) {
3030
}
3131

3232
auto main() -> int {
33-
std::println("Desktop path = \"{}\"", get_known_folder_path(CSIDL_DESKTOP));
33+
std::println("Desktop = \"{}\"", get_known_folder_path(CSIDL_DESKTOP));
3434
std::println("Program Files = \"{}\"", get_known_folder_path(CSIDL_PROGRAM_FILES));
3535
std::println("Home = \"{}\"", get_known_folder_path(CSIDL_HOME));
3636
}
@@ -39,34 +39,129 @@ auto main() -> int {
3939
```cpp
4040
// This code can produce the following output:
4141
//
42-
// Desktop path = "C:\Users\gammasoft71\Desktop"
42+
// Desktop = "C:\Users\gammasoft71\Desktop"
4343
// Program Files = "C:\Program Files"
4444
// Home = "C:\Users\gammasoft71"
4545
```
4646

47-
* La method `get_environment_variable ` retourne la valeur pour une variable d'environment donnée.
48-
* La method `get_known_folder_path` retourne le path pour une valeur CSIDL donnée.
47+
* This Win32 example demonstrates how to retrieve special folders by using system APIs.
48+
* For standard folders like Desktop or Program Files, we use `SHGetFolderPath`.
49+
* For Home, which doesn’t have a standard CSIDL, we construct the path from environment variables.
4950

5051
## Qt
5152

5253
```cpp
54+
#include <QApplication>
55+
#include <QStandardPaths>
56+
#include <QString>
57+
#include <QDebug>
5358

59+
#if defined(Q_OS_WIN)
60+
#include <shlobj.h>
61+
#pragma comment(lib, "shell32.lib")
62+
#endif
63+
64+
QTextStream& qCout() {
65+
static auto ts = QTextStream {stdout};
66+
return ts;
67+
}
68+
69+
QString programFilesPath() {
70+
#if defined(Q_OS_WIN)
71+
auto path = std::string(MAX_PATH, '\0');
72+
if (SHGetFolderPath(nullptr, CSIDL_PROGRAM_FILES, nullptr, SHGFP_TYPE_CURRENT, path.data()) != S_OK)
73+
throw std::runtime_error("Failed to get known folder path");
74+
return path.substr(0, path.find('\0'));
75+
#elif defined(Q_OS_MAC)
76+
return "/Applications";
77+
#else
78+
return QDir("/opt").exists() ? "/opt" : "/usr/local";
79+
#endif
80+
}
81+
82+
auto main(int argc, char* argv[]) -> int {
83+
auto application = QApplication {argc, argv};
84+
85+
qCout() << "Desktop = \"" << QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) << "\"" << Qt::endl;
86+
qCout() << "Program Files = \"" << programFilesPath() << "\"" << Qt::endl;
87+
qCout() << "Home = \"" << QStandardPaths::writableLocation(QStandardPaths::HomeLocation) << "\"" << Qt::endl;
88+
}
89+
```
90+
91+
```cpp
92+
// This code can produce the following output:
93+
//
94+
// Desktop = "C:\Users\gammasoft71\Desktop"
95+
// Program Files = "C:\Program Files"
96+
// Home = "C:\Users\gammasoft71"
97+
```
98+
99+
* This Qt example shows how to get special folders using the built-in `QStandardPaths` for Home and Desktop.
100+
* For Program Files, we still rely on a system call (`SHGetFolderPath`) on Windows, while macOS and Linux use conventional paths.
101+
* It illustrates that Qt simplifies some folders but isn’t fully unified across all platforms.
102+
103+
## xtd
104+
105+
```cpp
106+
#include <xtd/xtd>
107+
108+
auto main() -> int {
109+
console::write_line("Desktop = {}", environment::get_folder_path(environment::special_folder::desktop).quoted());
110+
console::write_line("Program Files = {}", environment::get_folder_path(environment::special_folder::program_files).quoted());
111+
console::write_line("Home = {}", environment::get_folder_path(environment::special_folder::home).quoted());
112+
}
54113
```
55114

56115
```cpp
57116
// This code can produce the following output:
58117
//
118+
// Desktop = "C:\Users\gammasoft71\Desktop"
119+
// Program Files = "C:\Program Files"
120+
// Home = "C:\Users\gammasoft71"
59121
```
60122

123+
* This xtd example demonstrates how to retrieve special folders in a fully cross-platform way.
124+
* With just three lines of code, we can get Desktop, Home, or Program Files without any platform-specific calls or `#ifdef` logic.
125+
* It highlights xtd’s simplicity and unified API compared to Win32 and Qt.
126+
61127
## Conclusion
62128

129+
Whether you use Win32, Qt or xtd, it is always possible to get the special folders.
130+
The main difference lies in:
131+
* Win32 : verbose, sometimes deprecated API, manual management of special cases (such as Home).
132+
* Qt : simplifies some folders (Home, Desktop) but still requires system calls for others (Program Files).
133+
* Xtd : unified, cross-platform, clear and readable approach, without #ifdef or explicit native calls.
134+
135+
With xtd, the focus is on the essentials: writing code that works everywhere, without worrying about the implementation details specific to each OS.
63136

64137
> **Did you know?**
65138
>
66-
>
139+
> * On Windows, the `Program Files` folder can change depending on the system language or if the user has moved it.
140+
> * On macOS, the concept of `Program Files` does not exist: the convention wants applications to be in `/Applications`.
141+
> * On Linux, there is no strict equivalent: `/opt` and `/usr/local` are often used for third-party applications.
142+
> * [xtd::environment::get_folder_path](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1environment.html#a465e71972e122d4bbdaa943ae02d4cd8) automatically manages these particularities according to the platform.
67143
68144
## To go further
69145

146+
* See [xtd::environment::special_folder](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1environment.html#a2fb2b59eded52f8745b88b44ae9e7057) documentation.
147+
* The following code shows how to simple combine path with special folder using [xtd::io::path::combine](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1io_1_1path.html#ae64bf12dad2f160ad85a951b9d011023) :
148+
149+
```cpp
150+
#include <xtd/xtd>
151+
152+
auto main() -> int {
153+
console::write_line("My target file = {}", path::combine(environment::get_folder_path(environment::special_folder::home), "MyAppData", "settings.txt").quoted());
154+
}
155+
156+
// This code can produce the following output on Windows:
157+
// My target file = "C:\Users\gammasoft71\MyAppData\settings.txt"
158+
//
159+
// This code can produce the following output on macOS:
160+
// My target file = "/Users/gammasoft71/MyAppData/settings.txt"
161+
//
162+
// This code can produce the following output on Linux:
163+
// My target file = "/home/gammasoft71/MyAppData/settings.txt"
164+
```
70165

71166
## See also
72167

0 commit comments

Comments
 (0)