PupaLib.FileIO 1.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package PupaLib.FileIO --version 1.3.0
                    
NuGet\Install-Package PupaLib.FileIO -Version 1.3.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="PupaLib.FileIO" Version="1.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PupaLib.FileIO" Version="1.3.0" />
                    
Directory.Packages.props
<PackageReference Include="PupaLib.FileIO" />
                    
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 PupaLib.FileIO --version 1.3.0
                    
#r "nuget: PupaLib.FileIO, 1.3.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 PupaLib.FileIO@1.3.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=PupaLib.FileIO&version=1.3.0
                    
Install as a Cake Addin
#tool nuget:?package=PupaLib.FileIO&version=1.3.0
                    
Install as a Cake Tool

<div align="center">

PupaLib.FileIO

C# Dotnet Nuget Github License

NuGet .NET

PupaLib.FileIO is an incredibly simple library for working with files and folders. 🎯

<img src="https://github.com/Artpupser/PupaLib.FileIO/blob/main/assets/banner.jpg" style="border-radius: 20px; max-height: 500px">

</div>

<div align="center">

📜 Content

</div>

🗝️ Key Features

🏆 Feature 📝 Description
Easy to Use Simplifies file manipulation tasks 🛠️
Versatile Functionality - Create new files and folders 📂<br>- Read and write data efficiently 📄
Cross-Platform Support Works seamlessly across different operating systems 🌍
Lightweight Minimal impact on performance ⚡

🚀 Installation

You can install the package through the NuGet Package Manager or via the command line

dotnet add package PupaLib.FileIO

Or through the NuGet Package Manager

Install-Package PupaLib.FileIO

💡 Usage

Here is a practical guide on how to use the VirtualFile, VirtualFolder, and VirtualIo classes to manage your file system operations efficiently.

🛠️ File and Folder Creation

Creating a folder:

var path = "./data/new_folder";
var folder = await VirtualFolder.GetOrCreateFolder(path); // Returns Option<VirtualFolder>

if (folder.Out(out var _folder))
{
    Console.WriteLine($"Folder created: {_folder.MyPath}");
}

Creating a file:

var filePath = "./data/test.txt";
var file = await VirtualFile.GetOrCreate(filePath); // Returns Option<VirtualFile>

if (file.Out(out var _file))
{
    Console.WriteLine($"File created: {_file.MyPath}");
}

Note: We use GetOrCreateFolder / GetOrCreate which handles both existing resources and creates new ones if they don't exist. The example above assumes you await the option and unwrap the successful result. If you want to use Option<VirtualFolder>, you can simply check if (folder) {}.

🔎 Check If File or Folder Exists

Check for a file:

var filePath = "./path/file_name.txt";
var file = VirtualFile.GetFile(filePath);

if (file.Out(out var _file))
{
    Console.WriteLine($"File exists, path -> {_file.MyPath}");
}
else
{
    Console.WriteLine("File does not exist");
}

Check for a folder:

var folderPath = "./path/folder_name";
var folder = VirtualFolder.GetFolder(folderPath);

if (folder.Out(out var _folder))
{
    Console.WriteLine($"Folder exists, path -> {_folder.MyPath}");
}
else
{
    Console.WriteLine("Folder does not exist");
}

📝 Write and Read Files

Writing to a text file:

var fileName = "message.txt";
var file = VirtualIo.RootFolder.GetFileIn(fileName);

if (file.Out(out var _file)) // Or use GetOrCreate if you want to create it if missing
{
    // Or use GetOrCreate if the file might not exist:
    // var file = VirtualFile.GetOrCreate(fileName).Content.Value; 

    var content = "Hello world!";
    await _file.WriteStringAsync(content); // Write file
    
    Console.WriteLine($"Writes content [{content}] in file");
    
    var readContent = await _file.ReadStringAsync(); // Read file
    Console.WriteLine($"File content: {readContent}");
    
    Console.WriteLine($"File size: {_file.SizeInBytes} bytes");
    Console.WriteLine($"Last modified: {_file.LastWriteTime}");
}

Writing with serialization:

var fileName = "user.json";
var file = VirtualIo.RootFolder.GetFileIn(fileName);

if (file.Out(out var _file))
{
    var content = new { name = "Alice", age = 30 }; // Anonymous type or custom class
    await _file.WriteTContentAsync(content, new JsonSystemSerializer()); // Write and serialize
    
    Console.WriteLine($"Writes content [\n{content}\n] in file");
    
    var readUser = await _file.ReadTContentAsync<Dictionary<string, object>>(new JsonSystemSerializer()); 
    // Note: Use specific type for ReadTContent, e.g., MyUserClass
    // var readUser = await _file.ReadTContentAsync<MyUser>(new JsonSystemSerializer());
    
    Console.WriteLine($"Read and deserialized content: {readUser}");
}

Alternative using GetOrCreate (Creates file if missing):

// Creates file if it doesn't exist, then writes content
var file = VirtualIo.RootFolder.GetOrCreateFileIn("data/data.bin").Content.Value; 
file.WriteBytes(new byte[] { 1, 2, 3 });

🗑️ Delete File and Folder

Deleting a file:

var fileName = "temp.txt";
var file = VirtualIo.RootFolder.GetFileIn(fileName);

if (file.Out(out var _file))
{
    _file.DeleteMe(); // Delete file
    Console.WriteLine($"File deleted, exists: {_file.Exists}"); // false
}

Deleting a folder (with recursive option):

var folderName = "old_data";
var folder = VirtualIo.RootFolder.GetFolderIn(folderName);

if (folder.Out(out var _folder))
{
    _folder.DeleteMe(full: true); // full = true deletes recursively
    Console.WriteLine($"Folder deleted, exists: {_folder.Exists}");
}

📊 Getting File/Folder Information

You can easily access metadata properties without writing code:

var file = VirtualIo.RootFolder.GetFileIn("test.txt");
if (file.Out(out var _file))
{
    Console.WriteLine($"Name: {_file.Name}");
    Console.WriteLine($"Extension: {_file.Extension}");
    Console.WriteLine($"Creation Time: {_file.CreationTime}");
    Console.WriteLine($"Size in Bytes: {_file.SizeInBytes}");
}

// For folders
var folder = VirtualIo.RootFolder.GetFolder("my_project");
if (folder.Out(out var _folder))
{
    Console.WriteLine($"Folder Name: {_folder.Name}");
    Console.WriteLine($"Total Files Count: {_folder.GetFilesCount()}");
    Console.WriteLine($"Total Folders Count: {_folder.GetDirectoriesCount()}");
}

🚀 Enumerating Contents

var rootFiles = VirtualIo.RootFolder.EnumerateFiles(SearchOption.AllDirectories);
foreach (var vFile in rootFiles)
{
    Console.WriteLine($"Found file: {vFile.MyPath}");
}

var subFolders = VirtualIo.RootFolder.EnumerateFolders(SearchOption.AllDirectories);
foreach (var vFolder in subFolders)
{
    Console.WriteLine($"Found folder: {vFolder.MyPath}");
}

🗃️ Devlog

1.3.0

  • Added: RestorePackagesWithLockFile field in PropertyGroup of .csproj files.
  • Added: PupaLib.Core dependency.
  • Changes: Introduced a new error handling system based on Option<T> (from PupaLib.Core) across the entire library.
  • Rewrite: Major refactoring to ensure VirtualFolder and VirtualFile methods return Option<T> results instead of null or throwing raw exceptions for missing files/folders.

1.2.1

  • Changed .NET SDK version, .net8.0 → .net10.0

⚖️ License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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 (1)

Showing the top 1 NuGet packages that depend on PupaLib.FileIO:

Package Downloads
PupaMVCF.Framework

Simplify MVC web framework on csharp

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.3.1 169 4/24/2026
1.3.0 103 4/24/2026
1.2.1 146 4/12/2026
1.2.0 987 3/17/2026
1.0.1 401 1/7/2026