How To Get System Folder Path(C:Windows C:Program Files) in Windows using C++?(如何使用 C++ 在 Windows 中获取系统文件夹路径(C:Windows C:Program Files)?)
问题描述
我正在用 C++ MFC 编程,
I am programming in c++ MFC,
我想获取C:windows"c:program files"文件夹路径.
I want to get "C:windows" "c:program files" folder path.
有时用户可能会在其他文件夹中设置窗口,例如 c:windows0.
是否有任何 API 来获取窗口的绝对路径和程序文件路径?
Sometimes user may setup windows in other folder such as c:windows0.
Is there any API to get absolute path of the windows and program files path?
非常感谢!
推荐答案
使用 Win32 API>
Using Win32 API>
对于 Windows 文件夹:
For the Windows folder:
TCHAR windir[MAX_PATH];
GetWindowsDirectory(windir, MAX_PATH);
对于程序文件:
TCHAR pf[MAX_PATH];
SHGetSpecialFolderPath(
0,
pf,
CSIDL_PROGRAM_FILES,
FALSE );
其中 MAX_PATH 来自 Windows 标头,将保证缓冲区足够长以容纳最长(非 UNC)路径.
Where MAX_PATH comes from the Windows headers and will guarantee the buffer is long enough for the longest (non-UNC) path.
另外,请注意 SHGetSpecialFolderPath 可用于检索其他特殊"文件夹,包括 Windows 文件夹,只需将第三个参数替换为此 列表.
Also, note that SHGetSpecialFolderPath can be used to retrieve other "special" folder including the Windows folder just by replacing the third parameter to any from this list.
这篇关于如何使用 C++ 在 Windows 中获取系统文件夹路径(C:Windows C:Program Files)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 C++ 在 Windows 中获取系统文件夹路径(C:Windows C:Program Files)?
基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
