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

时间:2021-05-30 02:29:45

I am programming in c++ MFC,

我在c++ MFC编程,

I want to get "C:\windows" "c:\program files" folder path.

我想要“C:\windows”“C:\程序文件”文件夹路径。

Sometimes user may setup windows in other folder such as c:\windows0.

有时用户可以在其他文件夹中设置窗口,比如c:\windows0。

Is there any API to get absolute path of the windows and program files path?

是否有任何API可以获得windows和程序文件路径的绝对路径?

Many thanks!

很多谢谢!

5 个解决方案

#1


20  

Using Win32 API>

使用Win32 API >

For the Windows folder:

Windows文件夹:

TCHAR windir[MAX_PATH];
GetWindowsDirectory(windir, MAX_PATH);

For program files:

程序文件:

TCHAR pf[MAX_PATH];
SHGetSpecialFolderPath(
    0,
    pf, 
    CSIDL_PROGRAM_FILES, 
    FALSE ); 

Where MAX_PATH comes from the Windows headers and will guarantee the buffer is long enough for the longest (non-UNC) path.

其中MAX_PATH来自于Windows header,并保证缓冲区足够长,可以用于最长的(非unc)路径。

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.

另外,请注意,可以使用SHGetSpecialFolderPath检索其他“特殊”文件夹,包括Windows文件夹,只需将第三个参数替换为该列表中的任何一个。

#2


10  

#3


1  

Most of these come from SHGetFolderPath, but GetSystemDirectory() returns the absolute location of C:\Windows\System32. Don't use GetWindowsDirectory(). It doesn't do what you want anymore.

其中大部分来自于SHGetFolderPath,但是GetSystemDirectory()返回的绝对位置是c:windowssystem32。不要使用GetWindowsDirectory()。它不再做你想做的事了。

#4


1  

On Vista+, SHGetKnownFolderPath is the replacement for SHGetFolderPath and SHGetSpecialFolderPath, although you can continue to use the older functions if you need backward compatibility to older versions of Windows.

在Vista+中,SHGetKnownFolderPath是SHGetFolderPath和SHGetSpecialFolderPath的替代品,但如果您需要向后兼容旧版本的Windows,则可以继续使用旧函数。

#5


0  

Call getenv("%ProgramFiles%") and getenv("%WinDir%")

调用getenv(% ProgramFiles %)和getenv(“%列出%”)

#1


20  

Using Win32 API>

使用Win32 API >

For the Windows folder:

Windows文件夹:

TCHAR windir[MAX_PATH];
GetWindowsDirectory(windir, MAX_PATH);

For program files:

程序文件:

TCHAR pf[MAX_PATH];
SHGetSpecialFolderPath(
    0,
    pf, 
    CSIDL_PROGRAM_FILES, 
    FALSE ); 

Where MAX_PATH comes from the Windows headers and will guarantee the buffer is long enough for the longest (non-UNC) path.

其中MAX_PATH来自于Windows header,并保证缓冲区足够长,可以用于最长的(非unc)路径。

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.

另外,请注意,可以使用SHGetSpecialFolderPath检索其他“特殊”文件夹,包括Windows文件夹,只需将第三个参数替换为该列表中的任何一个。

#2


10  

#3


1  

Most of these come from SHGetFolderPath, but GetSystemDirectory() returns the absolute location of C:\Windows\System32. Don't use GetWindowsDirectory(). It doesn't do what you want anymore.

其中大部分来自于SHGetFolderPath,但是GetSystemDirectory()返回的绝对位置是c:windowssystem32。不要使用GetWindowsDirectory()。它不再做你想做的事了。

#4


1  

On Vista+, SHGetKnownFolderPath is the replacement for SHGetFolderPath and SHGetSpecialFolderPath, although you can continue to use the older functions if you need backward compatibility to older versions of Windows.

在Vista+中,SHGetKnownFolderPath是SHGetFolderPath和SHGetSpecialFolderPath的替代品,但如果您需要向后兼容旧版本的Windows,则可以继续使用旧函数。

#5


0  

Call getenv("%ProgramFiles%") and getenv("%WinDir%")

调用getenv(% ProgramFiles %)和getenv(“%列出%”)