SHGetFolderPath Deprecated: What is alternative to retrieve path for Windows folders?
从Windows Vista开始不推荐使用
在Windows中检索应用程序文件夹路径的替代方法是什么?
1 | SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, 0, szPath) |
除此之外,为什么在使用此功能时出现这些错误:
1 2 3 | Error 1 error C2065: 'CSIDL_COMMON_APPDATA' : undeclared identifier Error 2 error C3861: 'SHGetFolderPath': identifier not found |
替代方法在链接到的文档中进行了描述。即它是
但是,
现在,您可以切换到
就个人而言,我不会让这种贬值过分担心您。 Microsoft以保持向后兼容性而闻名。不要指望
您在编辑中提出的另一个问题是如何调用
它被链接到顶部的SHGetKnownFolderPath。
在新API中,CSIDL_COMMON_APPDATA被FOLDERID_ProgramData替换。
当我向已经工作的解决方案中添加几个新的头文件时,我遇到了同样的错误。
我已经在调用
我尝试用
On adding
#include to the header file of the class callingSHGetFolderPath , the
errors ceased and the solution compiled successfully again.
如本页面所述,在Windows Vista或更高版本的操作系统上调用
在Microsoft中,备用名称为" SHGetKnownFolderPath"
https://docs.microsoft.com/zh-cn/windows/win32/api/shlobj_core/nf-shlobj_core-shgetfolderpatha
从我的angular来看,这些功能适用于c,c和类似语言。
从powershell,我刚刚阅读了注册表:
PS> cd hkcu:\\\\软件\\\\ Microsoft \\\\ Windows \\\\ CurrentVersion \\\\ Explorer \\\\
PS> dir
在这里浏览" Shell文件夹"和" User Shell文件夹"。
btw:这些用于获取值。我说那是相当安全的。设置值时,最好不要直接使用注册表,因为它会破坏您的一天。使用浏览器->这些"目录"的属性来移动它们,也将移动内容。不幸的是,我不知道在Powershell中使用它的钩子。
我已经在Windows 10 PC上将
Note As of Windows Vista, this function is merely a wrapper for
SHGetKnownFolderPath . TheCSIDL value is translated to its associated
KNOWNFOLDERID and thenSHGetKnownFolderPath is called. New
applications should use the known folder system rather than the older
CSIDL system, which is supported only for backward compatibility.
正如大卫·赫夫曼(David Heffman)在他的回答中指出的那样,微软有多年保持向后兼容性的历史,特别是当他们可以采用较旧的功能并将其重定向到具有适当参数的新功能时。
该函数的使用示例如下。此用法检索当前用户的用户文件夹(例如Windows 7下的" C:\\\\ Users \\\\ myuser \\\\ Documents"),然后使用
1 2 3 4 5 6 7 8 9 10 11 | TCHAR achDevice[MAX_PATH]; HRESULT hr; // include file ShlObj.h contains list of CSIDL defines however only a subset // are supported with Windows 7 and later. // for the 3rd argument, hToken, can be a specified Access Token or SSID for // a user other than the current user. Using NULL gives us the current user. if (SUCCEEDED(hr = SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, achDevice))) { // append a folder name to the user's Documents directory. // the Path Handling functions are pretty handy. PathAppend(achDevice, L"xxx"); } |
一个可能的失败是一个或多个无效参数(hr ==
有一些
Windows 7和更高版本对受支持的
另请参阅KNOWNFOLDERID,其中包括一个表,该表指示
1 2 3 4 5 6 7 8 | CSIDL_LOCAL_APPDATA - %USERPROFILE%\\AppData\\Local CSIDL_MYDOCUMENTS - %USERPROFILE%\\Document CSIDL_PERSONAL - %USERPROFILE%\\Documents CSIDL_FONTS - %windir%\\Fonts CSIDL_MYMUSIC - %USERPROFILE%\\Music CSIDL_MYPICTURES - %USERPROFILE%\\Pictures CSIDL_COMMON_APPDATA - %ALLUSERSPROFILE% (%ProgramData%, %SystemDrive%\\ProgramData) CSIDL_COMMON_DOCUMENTS - %PUBLIC%\\Documents |
顺便说一句,Shell路径处理函数是一个很好的方法库,用于处理文件路径。
另请参阅将通用可写应用程序文件放在何处?