xxxxxxxxxx
using System.Text.RegularExpressions;
// Specify the invalid filename you want to fix
string fileName = "example@file!name.txt";
// Define the regular expression pattern for characters that need to be replaced
string pattern = @"[^\w\-. ]";
// Replace invalid characters with valid ones
string fixedFileName = Regex.Replace(fileName, pattern, "_");
// Output the original and fixed filenames
Console.WriteLine("Original filename: {0}", fileName);
Console.WriteLine("Fixed filename: {0}", fixedFileName);