Add file size helper extension

This commit is contained in:
Matt Nadareski
2024-12-02 11:43:04 -05:00
parent 0a131c502e
commit 3141e1f020
2 changed files with 38 additions and 0 deletions

View File

@@ -6,6 +6,26 @@ namespace BinaryObjectScanner.Test
{
public class ExtensionsTests
{
#region FileSize
[Fact]
public void FileSize_Null_Invalid()
{
string? filename = null;
long actual = filename.FileSize();
Assert.Equal(-1, actual);
}
[Fact]
public void FileSize_Empty_Invalid()
{
string? filename = string.Empty;
long actual = filename.FileSize();
Assert.Equal(-1, actual);
}
#endregion
#region IterateWithAction
[Fact]

View File

@@ -1,10 +1,28 @@
using System;
using System.Collections.Generic;
using System.IO;
namespace BinaryObjectScanner
{
internal static class Extensions
{
/// <summary>
/// Helper to get the filesize from a path
/// </summary>
/// <returns>Size of the file path, -1 on error</returns>
public static long FileSize(this string? filename)
{
// Invalid filenames are ignored
if (string.IsNullOrEmpty(filename))
return -1;
// Non-file paths are ignored
if (!File.Exists(filename))
return -1;
return new FileInfo(filename).Length;
}
/// <summary>
/// Wrap iterating through an enumerable with an action
/// </summary>