I check a file name validation in the following way:
foreach (char c in System.IO.Path.GetInvalidFileNameChars())
{
validFileName = validFileName.Replace(c, '_');
}
Is there a faster way do do so?
I check a file name validation in the following way:
foreach (char c in System.IO.Path.GetInvalidFileNameChars())
{
validFileName = validFileName.Replace(c, '_');
}
Is there a faster way do do so?
Whats wrong with that? Two line implementation, and a stack overflow search for this shows your answer matches an accepted answer for another question How to make a valid Windows filename from an arbitrary string?
This might be faster, but I hardly think the original would be slow enough to care about...
var invalidChars = Path.GetInvalidFileNameChars();
var fileNameChars = validFileName.ToCharArray();
for (int i = 0; i < fileNameChars.Length; ++i)
if (invalidChars.Contains(fileNameChars[i]))
fileNameChars[i] = '_';
validFileName = new string(fileNameChars);