Refactored to use the long extension

This commit is contained in:
Thorsten Sommer 2025-01-23 12:58:23 +01:00
parent fa849d39a9
commit d3bc838477
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -2,20 +2,16 @@ namespace AIStudio.Tools;
public static class FileInfoExtensions public static class FileInfoExtensions
{ {
/// <summary>
/// Returns the file size in human-readable format.
/// </summary>
/// <param name="fileInfo">The file info object.</param>
/// <returns>The file size in human-readable format.</returns>
public static string FileSize(this FileInfo fileInfo) public static string FileSize(this FileInfo fileInfo)
{ {
if (!fileInfo.Exists) if (!fileInfo.Exists)
return "N/A"; return "N/A";
var size = fileInfo.Length; return fileInfo.Length.FileSize();
string[] sizes = { "B", "kB", "MB", "GB", "TB" };
var order = 0;
while (size >= 1024 && order < sizes.Length - 1)
{
order++;
size /= 1024;
}
return $"{size:0.##} {sizes[order]}";
} }
} }