如何使用 C++ 在 Windows 中获取系统文件夹路径(C:\Windows C:\Program Files)?

如何使用 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 可以获取 Windows 和程序文件路径的绝对路径?

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.