gpt4 book ai didi

c++ - 是否有 SHGetSpecialFolderLocation 的新替代品?

转载 作者:可可西里 更新时间:2023-11-01 18:27:48 35 4
gpt4 key购买 nike

我的应用程序是用适用于 Win32 的 C++Builder 编写的。我的代码使用 SHGetSpecialFolderLocation()用于获取 CSIDL_APPDATACSIDL_MYDOCUMENTS 路径的 API。

我在 2018 年 12 月 4 日的 Microsoft 网站上注意到它说:

[SHGetSpecialFolderLocation is not supported and may be altered or unavailable in the future. Instead, use SHGetFolderLocation.]

然后对于 SHGetFolderLocation 它说:

Deprecated

目前获取这两条路径的方式是什么?

我当前的代码如下。

LPITEMIDLIST List = NULL;
wchar_t wPath[MAX_PATH + 1];
UnicodeString S01, Fi;

if( !SHGetSpecialFolderLocation(0, CSIDL_APPDATA, &List) ){
if( SHGetPathFromIDListW(List, wPath ) ){
S01 = wPath;
Fi = (S01+"\\my_files\\");
Form1->MyRoamingPath_Mh = Fi;
}
}

最佳答案

SHGetSpecialFolderLocation()首次引入是在 Windws 95/NT4 中。它在 Windows 2000/XP 中被弃用,取而代之的是 SHGetFolderLocation() (返回文件夹位置作为 IDLIST_ABSOLUTE)和 SHGetFolderPath() (它将文件夹位置作为路径字符串返回)。

因此,在您的示例中,您可以改用 SHGetFolderPath():

#include <Shlobj.h>
#include <SysUtils.hpp>

wchar_t wPath[MAX_PATH + 1];

if (SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, wPath) == S_OK)
{
Form1->MyRoamingPath_Mh = IncludeTrailingPathDelimiter(wPath) + L"my_files\\";
}

在 Vista 中,使用 CSIDL已弃用 KNOWNFOLDERID .上述功能已被弃用,取而代之的是 SHGetKnownFolderIDList()/IKnownFolder::GetIDList()SHGetKnownFolderPath()/IKnownFolder::GetPath() , 分别。

这实际上在 SHGetFolderLocation() 文档 1底部中说明:

1:我猜你没有向下滚动到足够远看到它。

Note As of Windows Vista, this function is merely a wrapper for SHGetKnownFolderIDList. The CSIDL value is translated to its associated KNOWNFOLDERID and SHGetKnownFolderIDList is called. New applications should use the known folder system rather than the older CSIDL system, which is supported only for backward compatibility.

因此,在您的示例中,您现在可以改用 SHGetKnownFolderPath():

#include <Shlobj.h>
#include <SysUtils.hpp>

PWSTR pwPath;

if (SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, NULL, &pwPath) == S_OK)
{
try
{
Form1->MyRoamingPath_Mh = IncludeTrailingPathDelimiter(pwPath) + L"my_files\\";
}
__finally
{
CoTaskMemFree(pwPath);
}
}

对于“我的文档”文件夹,使用 FOLDERID_Documents

关于c++ - 是否有 SHGetSpecialFolderLocation 的新替代品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54297253/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com