2

我有一个文件夹的pidl(可能存在或已被删除)。

我可以IShellItem使用以下代码获得一个,但我需要的是获得该文件夹的创建日期。我想我可以得到它PKEY_DateCreated,但我不知道如何。

SHCreateShellItem(nil, nil, pidl, ShellItem);

我该怎么做呢 ?

我用德尔福。

4

3 回答 3

2

纯 Winapi 示例:

IShellItem2* pItem2 = NULL;
hr = pItem->QueryInterface(&pItem2);
if (SUCCEEDED(hr))
{
   FILETIME ft = {0};
   pItem2->GetFileTime(PKEY_DateCreated, &ft);
   SYSTEMTIME st = {0};
   ::FileTimeToSystemTime(&ft, &st);
   wprintf(
       L"Date Created: %04d-%02d-%02d %02d:%02d:%02d\n", 
       st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
   pItem2->Release();
}

正如 David Heffernan 指出的那样,您确定所有外壳项目都有创建日期吗?

翻译成德尔福,看起来像这样:

var
  Item: IShellItem;
  Item2: IShellItem2;
  ft: TFileTime;
  st: TSystemTime;
....
Item2 := Item as IShellItem2;
OleCheck(Item2.GetFileTime(PKEY_DateCreated, ft));
Win32Check(FileTimeToSystemTime(ft, st));
于 2013-03-02T21:31:49.560 回答
2

如果你有 PIDL,你可以使用SHGetDataFromIDList来获取对象的基本属性;你根本不需要IShellItem(或IShellItem2)。您可以为nFormat参数指定SHGDFIL_FINDDATA (请参阅SHGetDataFromIDList 详情)。

这样做的好处是,对于标准文件系统对象,元数据被编码在 PIDL 本身中,因此即使对象不再存在,该函数也会返回有用的数据。

于 2013-03-04T10:58:43.213 回答
2

获取创建日期

使用 IShellItem2

IShellItem2IShellItem通过添加一些辅助方法来从属性系统中检索类型化属性来扩展。

var
   ft: FILETIME;
   createdDate: TDateTime;

// IShellItem2 provides many handy helper methods to IShellItem
(shellItem as IShellItem2).GetFileTime(PKEY_DateCreated, {out}ft);

createdDate := FileTimeToDateTime(ft);

使用 IShellItem

Windows Vista Windows XP 1添加了将+IShellItem包装成单个对象的功能。Windows Vista 增加了丰富的属性系统,an是一组键值对属性(包括):[IShellFolder][ITEMID_CHILD]IPropertyStorePKEY_DateCreated

var
   ps: IPropertyStore;
   pv: TPropVariant;
   ft: FILETIME;
   createdDate: TDateTime;

//Get the IPropertyStore yourself, in order to read the property you want
shellItem.BindToHandler(nil, BHID_PropertyStore, IPropertyStore, {out}ps);
ps.GetValue(PKEY_DateCreated, {out}pv);  
PropVariantToFileTime(pv, PSTF_UTC, {out}ft);

createdDate := FileTimeToDateTime(ft);

使用 IShellFolder

IShellFolder是原来的Windows 95界面。它仅代表一个文件夹,您必须询问ITEMID_CHILD该文件夹中的项目( )。在 Vista 之前是没有的,因为在 Vista之前PKEY_DateCreated财产系统才存在。但是文件和文件夹仍然有CreationTime

var
   folder: IShellFolder;
   parent: PIDLIST_ABSOLUTE;
   child: PITEMID_CHILD;
   findData: WIN32_FIND_DATA;

// We have an IShellItem that represents IShellFolder+ChildItemID. 
// Ask the IShellItem to cough up its IShellFolder and child pidl
(shellItem as IParentAndItem).GetParentAndItem(parent, folder, child);

// Get the WIN32_FIND_DATA information associated with the child file/folder
SHGetDataFromIDList(folder, child, SHGDFIL_FINDDATA, findData, sizeof(findData));

createdDate := FileTimeToDateTime(findData.ftCreationTime);
于 2019-05-11T02:26:51.620 回答