I want to include a batch file rename functionality in my application. A user can type a destination filename pattern and (after replacing some wildcards in the pattern) I n
Regular expressions are overkill for this situation. You can use the String.IndexOfAny()
method in combination with Path.GetInvalidPathChars()
and Path.GetInvalidFileNameChars()
.
Also note that both Path.GetInvalidXXX()
methods clone an internal array and return the clone. So if you're going to be doing this a lot (thousands and thousands of times) you can cache a copy of the invalid chars array for reuse.