Safe/Allowed filename cleaner for .NET

.net, filenames, windows

Solution

This problem is not as simple as you may think. Not only are the characters in `Path.GetInvalidFileNameChars` illegal, there are several filenames, such as "PRN" and "CON", that are reserved by Windows and cannot be created. Any name that ends in "." is also illegal in Windows. Moreover, there are various length limitations. Read the full list here.

If that's not enough, different filesystems have different limitations, for example ISO 9660 filenames cannot start with "-" but can contain it.

Problem

Is there any standardized / libraried / tested way in .NET to to take an arbitrary string and mangle it in such a way that it represents a valid file name? Rolling my own char-replace function is easy enough, but I'd like something a little more robust and resued.

Original source

Related problems