<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/simplify-directory-file-operations-in-net-with-spargine-s-directoryinfoextensions-dotnettips-com-nplhkbolq" -->

---
title: Simplify Directory File Operations in .NET with...
description: Spargine's DirectoryInfoExtensions library adds four extension methods to .NET's DirectoryInfo class to simplify common file operations: CreateTempFileThenMove...
canonical: https://daily.dev/posts/simplify-directory-file-operations-in-net-with-spargine-s-directoryinfoextensions-dotnettips-com-nplhkbolq
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Simplify Directory File Operations in .NET with Spargine’s DirectoryInfoExtensions – dotNetTips.com | daily.dev
og:description: Spargine's DirectoryInfoExtensions library adds four extension methods to .NET's DirectoryInfo class to simplify common file operations: CreateTempFileThenMove...
og:url: https://daily.dev/posts/simplify-directory-file-operations-in-net-with-spargine-s-directoryinfoextensions-dotnettips-com-nplhkbolq
og:image: https://api.daily.dev/og/posts/nPLHKbOLQ.png
og:image:alt: Simplify Directory File Operations in .NET with Spargine’s DirectoryInfoExtensions – dotNetTips.com
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Simplify Directory File Operations in .NET with Spargine’s DirectoryInfoExtensions – dotNetTips.com

**[We Are .NET](https://daily.dev/sources/wearedotnet)** · 3 min read · 1 upvotes · 0 comments

## Summary

Spargine's DirectoryInfoExtensions library adds four extension methods to .NET's DirectoryInfo class to simplify common file operations: CreateTempFileThenMove for atomic writes via a temporary file, GetSize to sum file byte lengths (optionally recursive) while skipping IOExceptions on individual files, ReadAllTextSafe to read text with a fallback value on failure, and WriteAllTextAtomic as a shorthand for atomic text writes. The open-source library is available on GitHub and NuGet, maintained by its author since the .NET Framework 2 era and now targeting .NET 10.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://dotnettips.com/2026/09/30/simplify-directory-file-operations-in-net-with-spargines-directoryinfoextensions>

## Questions this post answers

### How can I write a text file atomically in .NET so I don't leave a partially written file if something fails?

Write the content to a temporary file first, then move it to the final destination once the write completes, which avoids leaving a partial file if the process is interrupted. Spargine's DirectoryInfoExtensions provides this via CreateTempFileThenMove and a shorthand WriteAllTextAtomic method, both of which create the destination directory if needed and return a refreshed FileInfo for the written file.

_daily.dev surfaces practical .NET patterns like atomic file writes for developers hardening file handling code._

### How do I calculate the total size of all files in a directory in .NET, including subdirectories, without crashing on unreadable files?

Sum the byte lengths of files matching a search pattern using a directory extension method that accepts SearchOption.AllDirectories to include subdirectories. If reading an individual file's length throws an IOException, that error is traced and the file is skipped so calculation continues with the remaining files, though errors during enumeration itself can still propagate to the caller.

_developers building file-size utilities in .NET can track library patterns like this through daily.dev._

## Similar posts on daily.dev

- [Coding Faster with dotNetTips.com Spargine 10: October 2026 Release](https://daily.dev/posts/coding-faster-with-dotnettips-com-spargine-10-october-2026-release-pnkpeahzr) · We Are .NET · 1 upvotes · 0 comments
- [Exploring C\# File-based Apps in .NET 10](https://daily.dev/posts/exploring-c-file-based-apps-in-net-10-yepr485nu) · Milan Jovanović · 12 upvotes · 1 comments
- [The best new features of C\# 14](https://daily.dev/posts/the-best-new-features-of-c-14-6gsbbjwv4) · InfoWorld · 46 upvotes · 4 comments
- [5 tiny tools fixed problems I didn't know were slowing me down](https://daily.dev/posts/5-tiny-tools-fixed-problems-i-didn-t-know-were-slowing-me-down-bt2c0hqj7) · XDA Developers · 0 upvotes · 0 comments
- [New features and Roslyn analyzers for Meziantou.Framework.FullPath](https://daily.dev/posts/new-features-and-roslyn-analyzers-for-meziantou-framework-fullpath-po20wwci9) · Meziantou · 6 upvotes · 0 comments

---

Tags: [#.net](https://daily.dev/tags/.net), [#c#](https://daily.dev/tags/c#), [#nuget](https://daily.dev/tags/nuget)

[View this post on daily.dev](https://daily.dev/posts/simplify-directory-file-operations-in-net-with-spargine-s-directoryinfoextensions-dotnettips-com-nplhkbolq)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Simplify Directory File Operations in .NET with Spargine’s DirectoryInfoExtensions – dotNetTips.com","url":"https://daily.dev/posts/simplify-directory-file-operations-in-net-with-spargine-s-directoryinfoextensions-dotnettips-com-nplhkbolq","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/simplify-directory-file-operations-in-net-with-spargine-s-directoryinfoextensions-dotnettips-com-nplhkbolq"},"datePublished":"2026-09-30T07:21:37.739Z","dateModified":"2026-09-30T07:22:14.904Z","description":"Spargine's DirectoryInfoExtensions library adds four extension methods to .NET's DirectoryInfo class to simplify common file operations: CreateTempFileThenMove...","image":"https://i0.wp.com/dotnettips.com/wp-content/uploads/2026/09/ChatGPT-Image-Sep-28-2026-04_13_09-PM.png?fit=1200%2C800&ssl=1","thumbnailUrl":"https://i0.wp.com/dotnettips.com/wp-content/uploads/2026/09/ChatGPT-Image-Sep-28-2026-04_13_09-PM.png?fit=1200%2C800&ssl=1","isAccessibleForFree":true,"articleSection":"We Are .NET","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"We Are .NET","logo":"https://media.daily.dev/image/upload/s--BsnVOa3i--/f_auto/v1718347410/logos/wearedotnet","url":"https://daily.dev/sources/wearedotnet"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/simplify-directory-file-operations-in-net-with-spargine-s-directoryinfoextensions-dotnettips-com-nplhkbolq","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":".net,c#,nuget","timeRequired":"PT3M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"We Are .NET","item":"https://daily.dev/sources/wearedotnet"},{"@type":"ListItem","position":3,"name":"Simplify Directory File Operations in .NET with Spargine’s DirectoryInfoExtensions – dotNetTips.com"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/simplify-directory-file-operations-in-net-with-spargine-s-directoryinfoextensions-dotnettips-com-nplhkbolq#faq","mainEntity":[{"@type":"Question","name":"How can I write a text file atomically in .NET so I don't leave a partially written file if something fails?","acceptedAnswer":{"@type":"Answer","text":"Write the content to a temporary file first, then move it to the final destination once the write completes, which avoids leaving a partial file if the process is interrupted. Spargine's DirectoryInfoExtensions provides this via CreateTempFileThenMove and a shorthand WriteAllTextAtomic method, both of which create the destination directory if needed and return a refreshed FileInfo for the written file. daily.dev surfaces practical .NET patterns like atomic file writes for developers hardening file handling code."}},{"@type":"Question","name":"How do I calculate the total size of all files in a directory in .NET, including subdirectories, without crashing on unreadable files?","acceptedAnswer":{"@type":"Answer","text":"Sum the byte lengths of files matching a search pattern using a directory extension method that accepts SearchOption.AllDirectories to include subdirectories. If reading an individual file's length throws an IOException, that error is traced and the file is skipped so calculation continues with the remaining files, though errors during enumeration itself can still propagate to the caller. developers building file-size utilities in .NET can track library patterns like this through daily.dev."}}]}
```

