Share via

GetInvalid-File-Path-NameChars

Peter Volz 1,295 Reputation points
2023-10-14T03:35:22.24+00:00

Hello

I've written this to check if a path name does not include invalid chars which is 2 loop inside each other:

For Each MyChar As Char In AddFolderTextBoxX.Text.Trim
    For Each BadChar As Char In Path.GetInvalidFileNameChars()
        If MyChar = BadChar Then
            MessageBox.Show("bad!!!"
            Exit Sub
        End If
    Next
Next

Can this be improved in any way to prevent inner loops? Or this is just fine?

Also which one is preferred?

If MyChar = BadChar Then...
or
If String.Equals(MyChar, BadChar, StringComparison.OrdinalIgnoreCase) = True Then

Thanks :)

Developer technologies | VB
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

KOZ6.0 6,810 Reputation points
2023-10-14T04:43:22.6666667+00:00

I think this is fine

If AddFolderTextBoxX.Text.Trim().IndexOfAny(Path.GetInvalidFileNameChars()) > -1 Then
    MessageBox.Show("bad!!!")
End If

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.