Skip to content

Latest commit

History

32 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

C# Extensions

Some useful code that you need from time to time

ByteUnit

Now available as a nuget package:
dotnet add package ByteUnit --version 1.0.2

  1. Calculate the length directly into the unit prefix which is appropriate:
varvalue=ByteUnit.FindUnit(1024);// value.Length = Your new length// value.Type = Your new unit prefix.
  1. Calculate e.g. 1024 KB to MB
varvalue=ByteUnit.From(newByteUnit(1024.0,Unit.KB),Unit.MB);// or alternativevarvalue=ByteUnit.From(ByteUnit.FromKB(1024),Unit.MB);
  1. Compare two ByteUnits
varfileSize=ByteUnit.FromFileInfo(newSystem.IO.FileInfo("PATH"));if(fileSize<ByteUnit.FromMB(10)){// e.g. only download this file if it's less than 10MB big.}
  1. Easy conversation
varvalue=ByteUnit.Parse("999.5 MB/s");vargb=value.To(Unit.GB);
  1. Parse from string
varvalue=ByteUnit.Parse("100 MB");// var value = ByteUnit.Parse("999.5 MB/s");// you can also use TryParse 

ByteUnit overrides the default ToString()-Method, so you can use it to format your values in strings. There is also an overload for "bytes per seconds", this will add /s to end of the formatted string!

FileAssociation

Useful for associate a file extension with your program (with icon)

// AddFileAssociation.SetFileAssociation("_NAME_","_EXTENSION_","_PATH_TO_ICO","_PATH_TO_EXE");// DeleteFileAssociation.DeleteFileAssociation("_NAME_","_EXTENSION_");

ZipHelper

// Create a zip archiveawaitZipHelper.CreateZipFileFromDirectoryAsync(@"F:\Data",@"C:\Users\test\Desktop\test.zip");// Extract a zip archiveawaitZipHelper.ExtractZipFileAsync(@"C:\Users\test\Desktop\test.zip",@"F:\Data");
  • You can also pass a byte[] to ExtractZipFileAsync(...)!
  • All methods are available synchronously (e.g. ExtractZipFiles(...)) and asynchronously (using async and await)!
  • If you are not using .NET Core there is no System.IO.Path.GetRelative Method. But you can use this port instead!

Hash

// Create hash from filestringhash=awaitHash.CreateHashFromFileAsync(@"F:\Data\test.iso");

About

Some useful code that you need from time to time

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Used by

Contributors

Languages