IsaacTruss.Util.FileHandler 1.1.0

dotnet add package IsaacTruss.Util.FileHandler --version 1.1.0
                    
NuGet\Install-Package IsaacTruss.Util.FileHandler -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="IsaacTruss.Util.FileHandler" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="IsaacTruss.Util.FileHandler" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="IsaacTruss.Util.FileHandler" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add IsaacTruss.Util.FileHandler --version 1.1.0
                    
#r "nuget: IsaacTruss.Util.FileHandler, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package IsaacTruss.Util.FileHandler@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=IsaacTruss.Util.FileHandler&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=IsaacTruss.Util.FileHandler&version=1.1.0
                    
Install as a Cake Tool

Overview

The FileHandler class provides an abstraction for managing and interacting with files in a secure and structured way. It includes functionalities for reading, writing, appending, and managing file properties. It also leverages FileIOPermission to check for access permissions.

Namespace

namespace fileHanding

Dependencies

  • System.IO: For file operations.
  • System.Security.Permissions: For permission checks.

Constructor

FileHandler(string path, string mode, bool createIfNotFound = true) Initializes a new instance of the FileHandler class.

Parameters:

  • path (string): The file path to manage.
  • mode (string): The mode in which the file is to be handled.
  • logpath (string): The path to where the logs are saved.
  • createIfNotFound (bool): Whether to create the file if it doesn't exist. Defaults to true.

Private Properties

  • logPath (string): Reserved for logging purposes.
  • path (string): The file path being managed.
  • mode (string): The operation mode of the file.
  • interactor (FileInteractor): Handles file-specific operations.
  • createIfNotFound (bool): Determines if the file should be created when missing.
  • properties (Dictionary<string, string>): Stores file metadata.
  • fileInfo (FileInfo): Stores information about the file.

Public Methods

1. File Information

Dictionary<string, string> getFileInfo()

Returns the stored file properties such as size, creation time, last access time, and last edit time.

2. Permission Checks

bool isReadable()

Checks if the file is readable.

bool isWriteable()

Checks if the file is writable.

bool isAppendable()

Checks if the file can be appended to.

Note: Each method uses FileIOPermission to verify access.

3. File Content Operations

void append(List<string> lines, bool writeLines = true)

Appends a list of lines to the file. Optionally allows toggling line-by-line writing.

void write(List<string> lines, bool writeLines = true)

Overwrites the file with the provided list of lines. Optionally allows toggling line-by-line writing.

string[] read()

Reads the file and returns its content as an array of strings.

string[,] readCSV()

Reads the file and returns its content as a 2D array of strings, should it be a CSV file.

4. File Management

void createFile()

Creates the file if it doesn't exist.

void deleteFile()

Deletes the file.

void moveFile(string newPath)

Moves the file to a new location and updates the file path.

void copyFile(string newPath)

Creates a copy of the file at the specified location.

Private Methods

void updateInfo()

Takes the metadata of the file (size, creation time, last access time, etc.) and stores it in the properties dictionary.

Notes

  1. FileIOPermission: Ensures secure file access by demanding appropriate permissions for read, write, and append operations.
  2. Error Handling: Exception handling should be added when deploying in production environments to handle unauthorized access or file-related errors gracefully.
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 114 11/29/2024
1.0.0 113 11/28/2024