Skip to content

Codeuctivity/SanitizeFilename

Repository files navigation

SanitizeFilename

Sanitizes file and directory names to ensure compatibility with Windows (NTFS), Linux (ext4), and macOS (APFS).

.NET build and test NuGet Donate

Implements rules documented by Microsoft + file name length truncation to 255 bytes - common on many modern file systems. Runs on any .NET platform.

Example

using Codeuctivity;

string unsafeString = "file*Name";
string safeFileName = unsafeString.SanitizeFilename();
Console.WriteLine($"Unsafe: {unsafeString}");
//Unsafe: file*Name
Console.WriteLine($"Sanitized: {safeFileName}");
//Sanitized: file_Name

string safeFileNameOptionalReplacementChar = unsafeString.SanitizeFilename(' ');
Console.WriteLine($"SafeFileNameOptionalReplacementChar: {safeFileNameOptionalReplacementChar}");
//SafeFileNameOptionalReplacementChar: file Name

Rules

Restrictions of Windows, Linux and OsX are alle combined to an replacement pattern, that will sanitize any filename to be compatible with any of the OS and common filesystem restrictions.

Pattern OS that don't support pattern OS that support pattern Example
Reserved keywords Windows Linux, OsX CON, PRN, AUX, ...
Reserved chars Linux, Windows, OsX '/', '\0'
Reserved chars windows Windows Linux, OsX '\', '""', ...
Invalid trailing chars Windows Linux, OsX ' ', ','
Max length Linux Linux, Windows, OsX 255 bytes
Max length Linux, Windows, OsX 255 chars
Unpaired Unicode surrogates OsX, Linux Windows U+D800 - U+DFFF
NotAssigned to Unicode OsX Linux, Windows U+67803, ...
"New" Unicode (today 16 + 17) OsX Linux, Windows 🫩 (U+1FAE9), ...

.NET framework support

  • Support for legacy .NET versions will be maintained as long as it is funded.
  • Support for .NET Framework 4.6.2 and higher was added in Version 2.0.145.
  • Edge case Unicode sanitization: .NET Framework uses Unicode 8.0, while .NET 8+ uses a newer version to detect unpaired surrogates and unassigned code points.
    • This is relevant when dealing with emoticons.
    • For example, "💏🏻" will be sanitized when running on .NET Framework 4.8, while it is supported as a valid filename on modern filesystems

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  

Packages

No packages published

Contributors 2

  •  
  •  

Languages