LFInteractive Docs
  • Programming Documentation
  • C#
    • Installing Visual Studio
    • Understanding C#
      • Namespaces
      • Classes
      • Enum
      • Variables
        • Static Types
        • Primitive Types
        • Access Modifiers
        • Variables vs Properties
        • Nullable Variables
      • Getters and Setters
      • Solution vs Project
      • Struct vs Class
      • Coding Conventions
      • Tasks and Async
        • Parallel Tasks
      • Methods
      • PreProcessor Statements
    • Creating Your First Console App
      • Class Library
        • Models
          • File Model
          • Result Model
        • Controllers
          • File Controller
          • File System Controller
      • Console App
        • Nuget Packages
        • Main Method
    • Minecraft.NET
      • Minecraft.NET Library
      • Modrinth.NET Library
      • CurseForge.NET Library
      • Fabric.NET Library
    • Common Lib
      • Strings
      • Advanced Network Client
      • Advanced Timer
      • Advanced File Info
      • Configuration File
      • Application Config
      • Database File
      • Crypt Class
  • C++
    • Networking
      • Windows Socket (Client)
    • cclip
    • VCPKG
    • spdlog
      • Getting Started
      • Patterns
Powered by GitBook
On this page
  • Namespace
  • Constructors
  • Properties
  • Methods
  • Enum
  • Example Usage

Was this helpful?

Edit on GitHub
  1. C#
  2. Common Lib

Advanced File Info

The AdvancedFileInfo class is part of the Chase CommonLib library, designed to provide additional information about a file, including its size representation in various units. This class extends the functionality of the standard System.IO.FileInfo class by adding methods to convert file sizes to human-readable strings in different units.

Namespace

namespace Chase.CommonLib.Math;

Constructors

AdvancedFileInfo(string file)

Creates a new instance of the AdvancedFileInfo class with the specified file path.

  • Parameters:

    • file (string): The path of the file.

Properties

BaseInfo (FileInfo)

The BaseInfo property holds the base System.IO.FileInfo object for the specified file path.

IsDirectory (bool)

The IsDirectory property indicates whether the specified path represents a directory (true) or not (false).

Methods

SizeToString(long bytes, int places = 2, FileSizeUnit unit = FileSizeUnit.Bytes)

Converts a file size in bytes to a human-readable string representation with the specified number of decimal places and unit.

  • Parameters:

    • bytes (long): The size of the file in bytes.

    • places (int, optional): The number of decimal places to round the size. Default is 2.

    • unit (FileSizeUnit, optional): The unit of file size to use for representation (Bytes, Bits, or IbiBytes). Default is Bytes.

  • Returns: (string) A human-readable string representation of the file size.

  • Exceptions:

    • ArgumentException: Thrown if an invalid unit value is provided.

SizeToString(int places = 2, FileSizeUnit unit = FileSizeUnit.Bytes)

Converts the size of the file represented by this AdvancedFileInfo object to a human-readable string representation with the specified number of decimal places and unit.

  • Parameters:

    • places (int, optional): The number of decimal places to round the size. Default is 2.

    • unit (FileSizeUnit, optional): The unit of file size to use for representation (Bytes, Bits, or IbiBytes). Default is Bytes.

  • Returns: (string) A human-readable string representation of the file size.

Enum

FileSizeUnit

An enum representing different file size units:

  • Bytes: Represents file size in bytes (e.g., 1.2 MB).

  • Bits: Represents file size in bits (e.g., 1.2 Mb).

  • IbiBytes: Represents file size in binary bytes (e.g., 1.2 MiB).

Example Usage

using Chase.CommonLib.Math;

class Program
{
    static void Main()
    {
        // Create an AdvancedFileInfo object for a file
        AdvancedFileInfo fileInfo = new AdvancedFileInfo("example.txt");

        // Check if the path is a directory
        if (fileInfo.IsDirectory)
        {
            Console.WriteLine("The path is a directory.");
        }
        else
        {
            Console.WriteLine("The path is not a directory.");
        }

        // Get and print the file size in different units
        Console.WriteLine("File Size (Bytes): " + fileInfo.SizeToString(unit: FileSizeUnit.Bytes));
        Console.WriteLine("File Size (Bits): " + fileInfo.SizeToString(unit: FileSizeUnit.Bits));
        Console.WriteLine("File Size (IbiBytes): " + fileInfo.SizeToString(unit: FileSizeUnit.IbiBytes));
    }
}

This example demonstrates how to use the AdvancedFileInfo class to check if a path is a directory and convert the file size to different units for a specified file.

PreviousAdvanced TimerNextConfiguration File

Last updated 1 year ago

Was this helpful?