如何使用 C++ 在 Windows 中获取系统文件夹路径(C:Windows C:Program Files)? [英] How To Get System Folder Path(C:Windows C:Program Files) in Windows using C++?

查看:20
本文介绍了如何使用 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)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆