C# function to remove illegal characters in a filenameWhenever we perform File Handling operation such as creating a file on disk so we need to generate a file name that can be generate on any logic. But some time that file name is not vaild and we got runtime errors.
So it is better to check the file name before creating the file.
I have written a small c# function to check and replace such illegal character to underscore from file name:
using System.IO;
private static string CheckFileName(string fileName)
{
foreach (char badChar in Path.GetInvalidFileNameChars())
{
fileName = fileName.Replace(badChar, '_');
}
return fileName;
}
Happy Coding..