.NET Fiddle

CSharp · Console

/Program.cs

using System;
using System.IO;
using System.Linq;
using System.Text;
					
public class Program
{
    enum ParserState {
        PossibleDriveLetter,
        PossibleDriveLetterSeparator,
        Path
    }
	
    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'))) {
                output.Append(current);
                if (state == ParserState.PossibleDriveLetter) {
                    state = ParserState.PossibleDriveLetterSeparator;
                }
                else {
                    state = ParserState.Path;
                }
            }
            else if ((current == Path.DirectorySeparatorChar) || (current == Path.AltDirectorySeparatorChar) ||
				((current == ':') && (state == ParserState.PossibleDriveLetterSeparator)) ||
                !Path.GetInvalidFileNameChars().Contains(current)) {

                output.Append(current);
                state = ParserState.Path;
            }
            else {
                output.Append('_');
                state = ParserState.Path;
            }
        }
        return output.ToString();
    }
    
	public static void Main()
	{
		Console.WriteLine(SanitizeFileName(@"C:\path\something\output_at_13:26:43.txt"));
	}
}
;