项目作者: ByteDev

项目描述 :
Set of IO related .NET utility classes.
高级语言: C#
项目地址: git://github.com/ByteDev/ByteDev.Io.git
创建时间: 2018-06-07T16:35:02Z
项目社区:https://github.com/ByteDev/ByteDev.Io

开源协议:MIT License

下载


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
  1. // Initialize object
  2. IFileSystem fs = new FileSystem();
  1. // Determine first part of path that exists
  2. var path = fs.GetPathExists(@"C:\Temp\ThisDoesntExist\test.txt");
  3. // path == "C:\Temp"
  1. // Is it a file? Is it a directory?
  2. bool isFile = fs.IsFile(@"C:\Temp\Something");
  3. bool isDir = fs.IsDirectory(@"C:\Temp\Something");
  1. // Return first thing that exists
  2. string[] paths =
  3. {
  4. @"C:\Temp\Test1.txt",
  5. @"C:\Temp\Test2.txt",
  6. @"C:\Temp\TestDirectory",
  7. };
  8. var path = fs.FirstExists(paths);
  1. // Return info on which paths exist and which don't
  2. string[] paths =
  3. {
  4. @"C:\Temp\Test1.txt",
  5. @"C:\Temp\Test2.txt",
  6. @"C:\Temp\TestDirectory",
  7. };
  8. var info = fs.Exists(paths);
  1. // Move a file
  2. string sourceFile = @"C:\Temp\TestFile1.txt";
  3. string destinationFile = @"C:\Windows\TestFile1.txt";
  4. FileInfo info = fs.MoveFile(sourceFile, destinationFile,
  5. FileOperationBehaviourType.DestExistsOverwrite);
  6. // info.FullName = @"C:\Windows\TestFile1.txt" if successful
  1. // Copy a file
  2. string sourceFile = @"C:\Temp\TestFile1.txt";
  3. string destinationFile = @"C:\Windows\TestFile1.txt";
  4. FileInfo info = fs.CopyFile(sourceFile, destinationFile,
  5. FileOperationBehaviourType.DestExistsOverwrite);
  6. // info.FullName = @"C:\Windows\TestFile1.txt" if successful
  1. // Swap two existing file's names
  2. string file1 = @"C:\Temp\archive1.png";
  3. string file2 = @"C:\Temp\cover.png";
  4. fs.SwapFileNames(file1, file2);

FileSize

Represents a file size as an object.

  1. long numberOfBytes = 1048576;
  2. FileSize fileSize = new FileSize(numberOfBytes, FileSize.MultiplierType.DecimalMultiplier);
  3. Console.Write(fileSize.ReadableSize); // "1 MB"
  4. Console.Write(fileSize.TotalBytes); // 1048576
  5. Console.Write(fileSize.TotalKiloBytes); // 1048
  6. 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
  1. var io = new IsolatedStorageIo(IsolatedStorageFileType.UserStoreForApplication);
  2. var fileName = new IsolatedStorageFileName("MyIsolatedFile", new Version(1, 0), ".txt");
  3. io.Write(fileName, "Some data");
  4. bool exists = io.Exists(fileName);
  5. string data = io.Read(fileName);
  6. io.Delete(fileName);

StreamFactory

Provides simple functionality to create memory streams from different input.

For example:

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

FileLocker

Simple way to manage the locking of files.

  1. using ByteDev.Io.Locking;
  2. // ...
  3. string file = @"C:\myfile.txt";
  4. // Lock a file (myfile.txt.lock is created)
  5. FileLockInfo fileLockInfo = FileLocker.Lock(file);
  6. // fileLockInfo.File == new FileInfo(@"C:\myfile.txt")
  7. // fileLockInfo.LockFile == new FileInfo(@"C:\myfile.txt.lock")
  8. // Determine if a file is created
  9. bool isLocked = FileLocker.IsLocked(file);
  10. // isLocked == true
  11. // Unlock a file (myfile.txt.lock is deleted)
  12. 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