Use GetInvalidFileNameChars to generate file names
- 2020-06-07 05:12:36
- OfStack
Sometimes it is necessary to generate a file name using a string retrieved from the database if the string contains disallowed characters (\,? ,
<
,
>
), you also need to replace each of these characters one by one
The code is as follows:
public static string GetValidName(string fileName)
{
foreach (char c in System.IO.Path.GetInvalidFileNameChars())
{
fileName = fileName.Replace(c, ' ');
}
return fileName;
}
In the same way, you can also determine if a string contains invalid characters.