我正在建立在线文件管理器。它显示的一个列是文件大小,但这总是很大的字节数。我想像Windows资源管理器一样显示文件大小,使用较小的数字和适当的单位,例如5 MB
代替5000000
。
对我来说这并不难,但我想知道Windows而不是内置函数来做到这一点。有没有东西,或者我必须自己动手?
答案 0 :(得分:4)
我看到3个变体:
function FormatFileSize(const ASize: UInt64; AKbMode: Boolean): UnicodeString;
var
PS: IPropertySystem;
PD: IPropertyDescription;
PV: TPropVariant;
Flags: DWORD;
Display: PWideChar;
PUI: IPropertyUI;
begin
Result := '';
// Variant 1
if Succeeded(CoCreateInstance(CLSID_IPropertySystem, nil, CLSCTX_INPROC_SERVER, IPropertySystem, PS)) then
begin
if Succeeded(PS.GetPropertyDescription(PKEY_Size, IPropertyDescription, PD)) then
begin
PV.vt := VT_UI8;
PV.uhVal.QuadPart := ASize;
if AKbMode then Flags := PDFF_ALWAYSKB
else Flags := PDFF_DEFAULT;
if Succeeded(PD.FormatForDisplay(PV, Flags, Display)) then
begin
Result := Display;
CoTaskMemFree(Display);
end;
PD := nil;
end;
PS := nil;
end;
if Result <> '' then Exit;
// Variant 2 - Windows XP mode, can be replaced with Variant 3
if Succeeded(CoCreateInstance(CLSID_PropertiesUI, nil, CLSCTX_INPROC_SERVER, IPropertyUI, PUI)) then
begin
PV.vt := VT_UI8;
PV.uhVal.QuadPart := ASize;
SetLength(Result, 100);
if Succeeded(PUI.FormatForDisplay(PKEY_Size.fmtid, PKEY_Size.pid, PV, PUIFFDF_DEFAULT, PWideChar(Result), Length(Result) + 1)) then
Result := PWideChar(Result)
else
Result := '';
PUI := nil;
end;
if Result <> '' then Exit;
// Variant 3
SetLength(Result, 100);
if AKbMode then
Result := StrFormatKBSizeW(ASize, PWideChar(Result), Length(Result))
else
Result := StrFormatByteSizeW(ASize, PWideChar(Result), Length(Result));
end;
答案 1 :(得分:1)
以下是C#中的两种变体(它们需要Windows Vista):
...
Console.WriteLine(FormatByteSize(1031023120)); // 983 MB
Console.WriteLine(FormatByteSize2(1031023120, true)); // 1 006 859 KB
...
请注意,使用Windows的好处(或不方便,具体取决于您的使用方法)是使用Shell / OS文化获得本地化版本(如果有)。
public static string FormatByteSize2(long size, bool alwaysKb = false)
{
// Here, we use Windows Shell's size column definition and formatting
// note although System.Size is defined as a UInt64, formatting doesn't support more than long.MaxValue...
PSGetPropertyKeyFromName("System.Size", out var pk);
var pv = new PROPVARIANT(size);
var sb = new StringBuilder(128);
const int PDFF_ALWAYSKB = 4;
PSFormatForDisplay(ref pk, pv, alwaysKb ? PDFF_ALWAYSKB : 0, sb, sb.Capacity);
return sb.ToString();
}
public static string FormatByteSize(long size)
{
// Here, we use use a Windows Shell API (probably the sames algorithm underneath)
// It's much simpler, we only need to declare one StrFormatByteSizeW API
var sb = new StringBuilder(128);
StrFormatByteSizeW(size, sb, sb.Capacity);
return sb.ToString();
}
[DllImport("shlwapi", CharSet = CharSet.Unicode)]
private static extern IntPtr StrFormatByteSizeW(long qdw, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszBuf, int cchBuf);
[DllImport("propsys", CharSet = CharSet.Unicode)]
private static extern int PSFormatForDisplay(
ref PROPERTYKEY propkey,
PROPVARIANT pv,
int pdfFlags,
[MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszBuf, int cchBuf);
[DllImport("propsys", CharSet = CharSet.Unicode)]
private static extern int PSGetPropertyKeyFromName([MarshalAs(UnmanagedType.LPWStr)] string pszName, out PROPERTYKEY ppropkey);
[StructLayout(LayoutKind.Sequential)]
private struct PROPERTYKEY
{
public Guid fmtid;
public int pid;
}
[StructLayout(LayoutKind.Sequential)]
private class PROPVARIANT
{
// note this version of PROPVARIANT is far from being suited for all purposes...
public short vt;
short wReserved1;
short wReserved2;
short wReserved3;
public long val;
const short VT_UI8 = 21;
public PROPVARIANT(long ul)
{
wReserved3 = wReserved2 = wReserved1 = 0;
val = ul;
vt = VT_UI8;
}
}