Skip to content

ByteDev/ByteDev.Io

Repository files navigation

Build status NuGet Package License: MIT

ByteDev.Io

Set of IO related .NET utility classes.

Installation

ByteDev.Io has been written as a .NET Standard 2.0 library, so you can consume it from a .NET Core or .NET Framework 4.6.1 (or greater) application.

ByteDev.Io is hosted as a package on nuget.org. To install from the Package Manager Console in Visual Studio run:

Install-Package ByteDev.Io

Further details can be found on the nuget page.

Release Notes

Releases follow semantic versioning.

Full details of the release notes can be viewed on GitHub.

Usage

To use these main public classes simply reference ByteDev.Io.

FileSytem

Provides a small set of methods for working with files and directories.

FileSytem methods:

  • GetPathExists
  • IsFile
  • IsDirectory
  • FirstExists
  • Exists
  • MoveFile
  • CopyFile
  • SwapFileNames
// Initialize object
IFileSystem fs = new FileSystem();
// Determine first part of path that exists

var path = fs.GetPathExists(@"C:\Temp\ThisDoesntExist\test.txt");

// path == "C:\Temp"
// Is it a file? Is it a directory?

bool isFile = fs.IsFile(@"C:\Temp\Something");
bool isDir = fs.IsDirectory(@"C:\Temp\Something");
// Return first thing that exists

string[] paths =
{
    @"C:\Temp\Test1.txt",
    @"C:\Temp\Test2.txt",
    @"C:\Temp\TestDirectory",
};

var path = fs.FirstExists(paths);
// Return info on which paths exist and which don't

string[] paths =
{
    @"C:\Temp\Test1.txt",
    @"C:\Temp\Test2.txt",
    @"C:\Temp\TestDirectory",
};

var info = fs.Exists(paths);
// Move a file

string sourceFile = @"C:\Temp\TestFile1.txt";
string destinationFile = @"C:\Windows\TestFile1.txt";

FileInfo info = fs.MoveFile(sourceFile, destinationFile, 
    FileOperationBehaviourType.DestExistsOverwrite);

// info.FullName = @"C:\Windows\TestFile1.txt" if successful
// Copy a file

string sourceFile = @"C:\Temp\TestFile1.txt";
string destinationFile = @"C:\Windows\TestFile1.txt";

FileInfo info = fs.CopyFile(sourceFile, destinationFile, 
    FileOperationBehaviourType.DestExistsOverwrite);

// info.FullName = @"C:\Windows\TestFile1.txt" if successful
// Swap two existing file's names

string file1 = @"C:\Temp\archive1.png";
string file2 = @"C:\Temp\cover.png";

fs.SwapFileNames(file1, file2);

FileSize

Represents a file size as an object.

long numberOfBytes = 1048576;

FileSize fileSize = new FileSize(numberOfBytes, FileSize.MultiplierType.DecimalMultiplier);

Console.Write(fileSize.ReadableSize);           // "1 MB"
Console.Write(fileSize.TotalBytes);             // 1048576
Console.Write(fileSize.TotalKiloBytes);         // 1048
Console.Write(fileSize.TotalMegaBytes);         // 1

FileComparer

Provides functionality to compare two files.

FileComparer methods:

  • IsSourceBigger
  • IsSourceBiggerOrEqual
  • IsSourceModifiedMoreRecently

IsolatedStorageIo

Provides functionality for isolated storage operations. To use reference namespace: ByteDev.Io.IsolatedStorage.

IsolatedStorageIo methods:

  • Exists
  • Delete
  • Write
  • Read
  • ReadAsXmlDoc
  • ReadAsXDoc
var io = new IsolatedStorageIo(IsolatedStorageFileType.UserStoreForApplication);

var fileName = new IsolatedStorageFileName("MyIsolatedFile", new Version(1, 0), ".txt");

io.Write(fileName, "Some data");

bool exists = io.Exists(fileName);

string data = io.Read(fileName);

io.Delete(fileName);

StreamFactory

Provides simple functionality to create memory streams from different input.

For example:

MemoryStream stream = StreamFactory.Create("some text");

FileLocker

Simple way to manage the locking of files.

using ByteDev.Io.Locking;

// ...

string file = @"C:\myfile.txt";

// Lock a file (myfile.txt.lock is created)
FileLockInfo fileLockInfo = FileLocker.Lock(file);

// fileLockInfo.File == new FileInfo(@"C:\myfile.txt")
// fileLockInfo.LockFile == new FileInfo(@"C:\myfile.txt.lock")

// Determine if a file is created
bool isLocked = FileLocker.IsLocked(file);

// isLocked == true

// Unlock a file (myfile.txt.lock is deleted)
FileLocker.Unlock(file);

Extension Methods

  • DirectoryInfo
    • CreateDirectory
    • DeleteIfExists
    • DeleteIfEmpty
    • DeleteDirectories
    • DeleteDirectoriesWithName
    • DeleteEmptyDirectories
    • DeleteFiles
    • DeleteFilesExcept
    • Empty
    • EmptyIfExists
    • GetAudioFiles
    • GetFilesByExtensions
    • GetImageFiles
    • GetLastModifiedFile
    • GetSize
    • GetVideoFiles
    • IsEmpty
  • FileInfo
    • AddExtension
    • DeleteIfExists
    • DeleteLine
    • DeleteLines
    • GetExtension
    • GetNextAvailableFileName
    • HasExtension
    • IsBinary
    • RenameExtension
    • RemoveExtension
    • ReplaceLine
    • ReplaceLines
  • Stream
    • IsEmpty
    • ReadAsBase64
    • ReadAsBytes
    • ReadAsMemoryStream
    • ReadAsString
    • WriteToFile
    • WriteToFileAsync
  • StreamReader
    • ReadLineKeepNewLineChars