{"id":4925,"date":"2022-06-27T11:59:00","date_gmt":"2022-06-27T16:59:00","guid":{"rendered":"https:\/\/neosmart.net\/blog\/?p=4925"},"modified":"2022-06-27T12:52:53","modified_gmt":"2022-06-27T17:52:53","slug":"c-file-size-formatting-library-prettysize-3-1-0-released","status":"publish","type":"post","link":"https:\/\/neosmart.net\/blog\/c-file-size-formatting-library-prettysize-3-1-0-released\/","title":{"rendered":"C# file size formatting library PrettySize 3.1 released"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignright size-thumbnail wp-image-4074 colorbox-4925\" src=\"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size-150x150.png\" alt=\"PrettySize\" width=\"150\" height=\"150\" srcset=\"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size-150x150.png 150w, https:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size-600x600.png 600w, https:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size-1024x1024.png 1024w, https:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size-300x300.png 300w, https:\/\/neosmart.net\/blog\/wp-content\/uploads\/mannequin-size.png 1200w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/>Hot on the heels of <a href=\"https:\/\/neosmart.net\/blog\/2022\/prettysize-0-3-release-and-a-weakness-in-rusts-type-system\/\" rel=\"follow\">an update to our rust port of PrettySize<\/a> we have a new release of PrettySize.NET that brings new features and capabilities to the best .NET library for formatting file sizes for human-readable output and display.<\/p>\n<p>PrettySize 3.1, available <a href=\"https:\/\/github.com\/neosmart\/PrettySize.net\" rel=\"nofollow\">on GitHub<\/a> and <a href=\"https:\/\/www.nuget.org\/packages\/NeoSmart.PrettySize\" rel=\"follow\">via Nuget<\/a>, has just been released and contains a number of improvements and requested features and newfound abilities to make handling file sizes (and not just formatting them) easier and more enjoyable.<\/p>\n<p><!--more--><\/p>\n<p>Starting with a quick recap of PrettySize&#8217;s abilities for those of you that haven&#8217;t heard of it before:<\/p>\n<pre><code class=\"language-csharp\"><span class=\"pl-k\">using<\/span> <span class=\"pl-en\">NeoSmart<\/span>.<span class=\"pl-en\">PrettySize<\/span>;\r\n\r\n<span class=\"pl-k\">public<\/span> <span class=\"pl-k\">void<\/span> <span class=\"pl-en\">Main<\/span>() {\r\n    <span class=\"pl-c\">\/\/ Initializing directly with a raw byte count:<\/span>\r\n    <span class=\"pl-k\">var<\/span> <span class=\"pl-en\">size<\/span> <span class=\"pl-k\">=<\/span> <span class=\"pl-k\">new<\/span> <span class=\"pl-en\">PrettySize<\/span>(bytes: <span class=\"pl-c1\">200<\/span>);\r\n    <span class=\"pl-smi\">Console<\/span>.<span class=\"pl-en\">WriteLine<\/span>(<span class=\"pl-s\"><span class=\"pl-pds\">$\"<\/span>Size: {<span class=\"pl-smi\">size<\/span>}<span class=\"pl-pds\">\"<\/span><\/span>); \r\n<span class=\"pl-c\">    \/\/ Prints \"Size: 200 bytes\"<\/span>\r\n\r\n    <span class=\"pl-c\">\/\/ Initializing via a unit-based helper function:<\/span>\r\n    <span class=\"pl-k\">var<\/span> <span class=\"pl-en\">size1<\/span> <span class=\"pl-k\">=<\/span> <span class=\"pl-smi\">PrettySize<\/span>.<span class=\"pl-en\">KiB<\/span>(<span class=\"pl-c1\">28<\/span>);\r\n    <span class=\"pl-k\">var<\/span> <span class=\"pl-en\">size2<\/span> <span class=\"pl-k\">=<\/span> <span class=\"pl-smi\">PrettySize<\/span>.<span class=\"pl-en\">Bytes<\/span>(<span class=\"pl-c1\">14336<\/span>);\r\n    <span class=\"pl-k\">var<\/span> <span class=\"pl-en\">sum<\/span> <span class=\"pl-k\">=<\/span> <span class=\"pl-smi\">size1<\/span> <span class=\"pl-k\">+<\/span> <span class=\"pl-smi\">size2<\/span>;\r\n    <span class=\"pl-smi\">Console<\/span>.<span class=\"pl-en\">WriteLine<\/span>(<span class=\"pl-s\"><span class=\"pl-pds\">$\"<\/span>The total size is {<span class=\"pl-smi\">sum<\/span>}<span class=\"pl-pds\">\"<\/span><\/span>);\r\n<span class=\"pl-c\">    \/\/ Prints \"The total size is 42.00 KiB\"<\/span>\r\n}\r\n<\/code><\/pre>\n<p>As you can see, PrettySize takes any arbitrary file size (in whatever unit you happen to have on hand) and automatically figures out how it should be displayed, figuring out<\/p>\n<ul>\n<li>What the result of any mathematical operations on strongly-typed <code>PrettySize<\/code> values (wrapping file sizes) are<\/li>\n<li>What unit to display the final size in (bytes, kilobytes, megabytes, etc)<\/li>\n<li>What precision to use for the shown numeric size (just enough, but not too much)<\/li>\n<\/ul>\n<p>New to version 3.x are the following:<\/p>\n<ul>\n<li>The ability to perform math directly on <code>PrettySize<\/code> types, including adding or subtracting <code>PrettySize<\/code> values and multiplying or dividing a <code>PrettySize<\/code> value by a scalar numeric value.<sup id=\"rf1-4925\"><a href=\"#fn1-4925\" title=\"In the case of multiplication, the commutative inverse (multiplying a scalar numeric value by a strongly-typed PrettySize value) is also supported. For more about the caveats when implementing commutative mathematical operations on or between arbitrary types, read last week&rsquo;s article on the rust release of PrettySize for some PLT-related matters.\" rel=\"footnote\">1<\/a><\/sup><\/li>\n<li>The ability to encapsulate\/express negative sizes (such as the negative result of <code>4KiB - 8KiB<\/code>),<\/li>\n<li>The ability to format\/print negative results, previously only available for unsigned values,<\/li>\n<li>Comparison and equality operators for directly comparing <code>PrettySize<\/code> values rather than having to compare their underlying byte count (as exposed via the <code>PrettySize.TotalBytes<\/code> property).<\/li>\n<\/ul>\n<p>There are some more improvements and enhancements under the hood, and a number of new helper functions modeled after the unit names to make it easier to instantiate <code>PrettySize<\/code> instances from non-byte file size values (e.g. directly from kilobytes). The interface should be more natural and ergonomic, and hopefully developers should find themselves reaching for <code>PrettySize.TotalBytes<\/code> far less than before.<\/p>\n<div class=\"sendy_widget\" style='margin-bottom: 0.5em;'>\n<p><em>If you would like to receive a notification the next time we release a nuget package for .NET or release resources for .NET Core and ASP.NET Core, you can subscribe below. Note that you'll only get notifications relevant to .NET programming and development by NeoSmart Technologies. If you want to receive email updates for all NeoSmart Technologies posts and releases, please sign up in the sidebar to the right instead.<\/em><\/p>\n<iframe tabIndex=-1 onfocus=\"sendy_no_focus\" src=\"https:\/\/neosmart.net\/sendy\/subscription?f=BUopX8f2VyLSOb892VIx6W4BB8V5K2ReYGLVwsfKUZLXCc892Ffz8rIgRyIGoE22cZVr&title=Join+the+dotnet+mailing+list\" style=\"height: 300px; width: 100%;\"><\/iframe>\n<\/div>\n<script type=\"text\/javascript\">function sendy_no_focus(e) { e.preventDefault(); }<\/script>\n<p>Remember that PrettySize supports custom formatting of file sizes and has full support for both base-10 (kilobyte, megabyte, gigabyte) and base-2 (kibibyte, mebibyte, gibibyte &#8212; more often referred to as KiB, MiB, GiB, etc) units and you can choose between them, as well as determine how the unit names are styled and more:<\/p>\n<pre><code class=\"language-csharp\"><span class=\"pl-c\">\/\/ using NeoSmart.PrettySize\r\n<\/span>\r\n<span class=\"pl-k\">var<\/span> <span class=\"pl-en\">size<\/span> <span class=\"pl-k\">=<\/span> <span class=\"pl-smi\">PrettySize<\/span>.<span class=\"pl-en\">Bytes<\/span>(<span class=\"pl-c1\">2048<\/span>);\r\n\r\n<span class=\"pl-k\">var<\/span> <span class=\"pl-en\">formatted<\/span> <span class=\"pl-k\">=<\/span> <span class=\"pl-smi\">size<\/span>.<span class=\"pl-en\">Format<\/span>(<span class=\"pl-smi\">UnitBase<\/span>.<span class=\"pl-smi\">Base2<\/span>, <span class=\"pl-smi\">UnitStyle<\/span>.<span class=\"pl-smi\">Full<\/span>);\r\n<span class=\"pl-smi\">Console<\/span>.<span class=\"pl-en\">WriteLine<\/span>(<span class=\"pl-smi\">formatted<\/span>); <span class=\"pl-c\">\/\/ Prints \"2.00 Kebibytes\"<\/span>\r\n\r\n<span class=\"pl-k\">var<\/span> <span class=\"pl-en\">formatted2<\/span> <span class=\"pl-k\">=<\/span> <span class=\"pl-smi\">size<\/span>.<span class=\"pl-en\">Format<\/span>(<span class=\"pl-smi\">UnitBase<\/span>.<span class=\"pl-smi\">Base10<\/span>, <span class=\"pl-smi\">UnitStyle<\/span>.<span class=\"pl-smi\">FullLower<\/span>);\r\n<span class=\"pl-smi\">Console<\/span>.<span class=\"pl-en\">WriteLine<\/span>(<span class=\"pl-smi\">formatted2<\/span>); <span class=\"pl-c\">\/\/ Prints \"2.05 kilobytes\"<\/span>\r\n<\/code><\/pre>\n<p class=\"info\"><b>Minor Update<\/b><br \/>\nVersion 3.1.1 is available on NuGet and includes a <code>[Obsolete]<\/code> backwards compatibility shim for a member that was misspelled in an earlier release.<\/p>\n<p>The code is released as open source under the MIT license and the library is available free of charge for all uses, commercial or otherwise. Contributions <a href=\"https:\/\/github.com\/neosmart\/PrettySize.net\" rel=\"nofollow\">to the GitHub repository<\/a> are welcome.<\/p>\n<p class=\"save\"><a href=\"https:\/\/www.nuget.org\/packages\/NeoSmart.PrettySize\" rel=\"follow\">Get PrettySize<\/a> (free, cross-platform)<\/p>\n<p>If you want to hear more from me about dotnet dev, <a href=\"https:\/\/twitter.com\/mqudsi\" rel=\"follow\">follow me on twitter<\/a>. To express your thanks or support, please <a href=\"https:\/\/github.com\/neosmart\/PrettySize.net\" rel=\"nofollow\">star the repo on GitHub<\/a> or retweet below:<\/p>\n<blockquote class=\"twitter-tweet\">\n<p lang=\"en\" dir=\"ltr\">Just released PrettySize v3, my <a href=\"https:\/\/twitter.com\/hashtag\/opensource?src=hash&amp;ref_src=twsrc%5Etfw\" rel=\"follow\">#opensource<\/a> C# and <a href=\"https:\/\/twitter.com\/dotnet?ref_src=twsrc%5Etfw\" rel=\"follow\">@dotnet<\/a> library for calculating and pretty-printing file sizes. <a href=\"https:\/\/t.co\/Dx2I7gqAMC\" rel=\"follow\">https:\/\/t.co\/Dx2I7gqAMC<\/a> <a href=\"https:\/\/twitter.com\/hashtag\/nuget?src=hash&amp;ref_src=twsrc%5Etfw\" rel=\"follow\">#nuget<\/a><\/p>\n<p>&mdash; Mahmoud Al-Qudsi (@mqudsi) <a href=\"https:\/\/twitter.com\/mqudsi\/status\/1541473211753316352?ref_src=twsrc%5Etfw\" rel=\"follow\">June 27, 2022<\/a><\/p><\/blockquote>\n<p> <script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n<hr class=\"footnotes\"><ol class=\"footnotes\"><li id=\"fn1-4925\"><p>In the case of multiplication, the commutative inverse (multiplying a scalar numeric value by a strongly-typed <code>PrettySize<\/code> value) is also supported. For more about the caveats when implementing commutative mathematical operations on or between arbitrary types, <a href=\"https:\/\/neosmart.net\/blog\/2022\/prettysize-0-3-release-and-a-weakness-in-rusts-type-system\/\" rel=\"follow\">read last week&#8217;s article<\/a> on the rust release of PrettySize for some PLT-related matters.&nbsp;<a href=\"#rf1-4925\" class=\"backlink\" title=\"Jump back to footnote 1 in the text.\">&#8617;<\/a><\/p><\/li><\/ol>","protected":false},"excerpt":{"rendered":"<p>Calculate and pretty-print file sizes with PrettySize.NET, the free C# file size formatting library. Version 3.0 is faster and easier-to-use. <a href=\"https:\/\/neosmart.net\/blog\/c-file-size-formatting-library-prettysize-3-1-0-released\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":505,"featured_media":4932,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[999],"tags":[91,325,963,52,964,1022],"class_list":["post-4925","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","tag-net","tag-c","tag-nuget","tag-open-source","tag-prettysize","tag-release"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/2022\/06\/C-PrettySize.png","jetpack_shortlink":"https:\/\/wp.me\/p4xDa-1hr","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/4925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/users\/505"}],"replies":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/comments?post=4925"}],"version-history":[{"count":13,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/4925\/revisions"}],"predecessor-version":[{"id":4939,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/4925\/revisions\/4939"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/media\/4932"}],"wp:attachment":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/media?parent=4925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/categories?post=4925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/tags?post=4925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}