SHGetSpecialFolderLocation 有新的替代品吗? [英] Is there a new replacement for SHGetSpecialFolderLocation?

查看:30
本文介绍了SHGetSpecialFolderLocation 有新的替代品吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My app is written in C++Builder for Win32. My code uses the SHGetSpecialFolderLocation() API to get the CSIDL_APPDATA and CSIDL_MYDOCUMENTS paths.

我在日期为 12/04/2018 的 Microsoft 网站上注意到它说:

I noticed on Microsoft's website dated 12/04/2018 that it says:

[SHGetSpecialFolderLocation 不受支持,将来可能会更改或不可用.而是使用 SHGetFolderLocation.]

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

然后对于 SHGetFolderLocation 它说:

已弃用

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

What is the current way to get these two paths?

我当前的代码如下.

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()(返回文件夹位置作为路径字符串).

SHGetSpecialFolderLocation() was first introduced in Windws 95/NT4. It was deprecated in Windows 2000/XP in favor of SHGetFolderLocation() (which returns a folder location as an IDLIST_ABSOLUTE) and SHGetFolderPath() (which returns a folder location as a path string).

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

So, in your example, you could have used SHGetFolderPath() instead:

#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(),分别.

In Vista, use of CSIDL was deprecated in favor of KNOWNFOLDERID. The above functions have been deprecated in favor of SHGetKnownFolderIDList()/IKnownFolder::GetIDList() and SHGetKnownFolderPath()/IKnownFolder::GetPath(), respectively.

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

This is actually stated at the bottom of the SHGetFolderLocation() documentation 1:

1:我猜你没有向下滚动到足够远的地方.

注意 从 Windows Vista 开始,此函数只是 SHGetKnownFolderIDList 的包装器.CSIDL 值被转换为其关联的 KNOWNFOLDERID 并调用 SHGetKnownFolderIDList.新应用程序应使用已知文件夹系统,而不是旧的 CSIDL 系统,后者仅支持向后兼容.

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() 代替:

So, in your example, you can now use SHGetKnownFolderPath() instead:

#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.

For the "My Documents" folder, use FOLDERID_Documents.

这篇关于SHGetSpecialFolderLocation 有新的替代品吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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