How to retrieve an IShellItem's file size?
See the question and my original answer on StackOverflowYou don't need to use IPropertyStore if you have an IShellItem2 reference, you can directly use IShellItem2::GetUInt64 . Here is some sample code:
CoInitialize(NULL);
...
IShellItem2* item;
if (SUCCEEDED(SHCreateItemFromParsingName(L"c:\\myPath\\myFile.ext", NULL, IID_PPV_ARGS(&item))))
{
ULONGLONG size;
if (SUCCEEDED(item->GetUInt64(PKEY_Size, &size))) // include propkey.h
{
... use size ...
}
item->Release();
}
...
CoUninitialize();
If you already have an IShellItem
reference (in general you want to get an IShellItem2
directly) and want a IShellItem2
, you can do this:
IShellItem2* item2;
if (SUCCEEDED(item->QueryInterface(&item2)))
{
... use IShellItem2 ...
}
Another way of doing it, w/o using IShellItem2
, is this:
IShellItem* item;
if (SUCCEEDED(SHCreateItemFromParsingName(L"c:\\myPath\\myFile.ext", NULL, IID_PPV_ARGS(&item))))
{
IPropertyStore* ps;
if (SUCCEEDED(item->BindToHandler(NULL, BHID_PropertyStore, IID_PPV_ARGS(&ps))))
{
PROPVARIANT pv;
PropVariantInit(&pv);
if (SUCCEEDED(ps->GetValue(PKEY_Size, &pv))) // include propkey.h
{
ULONGLONG size;
if (SUCCEEDED(PropVariantToUInt64(pv, &size))) // include propvarutil.h
{
... use size ...
}
PropVariantClear(&pv);
}
ps->Release();
}
item->Release();
}