PossibleDriveLetterSeparator,
static string SanitizeFileName(string input) {
StringBuilder output = new StringBuilder(input.Length);
ParserState state = ParserState.PossibleDriveLetter;
foreach(char current in input) {
if (((current >= 'a') && (current <= 'z')) || ((current >= 'A') && (current <= 'Z'))) {
if (state == ParserState.PossibleDriveLetter) {
state = ParserState.PossibleDriveLetterSeparator;
state = ParserState.Path;
else if ((current == Path.DirectorySeparatorChar) || (current == Path.AltDirectorySeparatorChar) ||
((current == ':') && (state == ParserState.PossibleDriveLetterSeparator)) ||
!Path.GetInvalidFileNameChars().Contains(current)) {
state = ParserState.Path;
state = ParserState.Path;
return output.ToString();
public static void Main()
Console.WriteLine(SanitizeFileName(@"C:\path\something\output_at_13:26:43.txt"));