mirror of
https://github.com/SabreTools/BinaryObjectScanner.git
synced 2026-02-15 05:36:03 +00:00
Add file size helper extension
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user