Illegal Filename characters

  • Thread starter Thread starter Seth Williams
  • Start date Start date
S

Seth Williams

I'm trying to build a SAVE AS routine, like MSWord does -
I select and grab the first line of the document and Puts it in the SAVE AS
filename dialog

However, if there are illegal characters, this won't work correctly.

How can I check, ahead of time, if the selection I've made has these illegal
characters?
 
How can I check, ahead of time, if the selection I've made has these
illegal characters?

I have this in my library:

public static bool FolderNameIsLegal(string folderName)
{
return (folderName.IndexOfAny(Path.GetInvalidPathChars()) == -1);
}

Path.GetInvalidPathChars() may help you?
 
Seth Williams said:
I'm trying to build a SAVE AS routine, like MSWord does -
I select and grab the first line of the document and Puts it in the SAVE
AS filename dialog

However, if there are illegal characters, this won't work correctly.

How can I check, ahead of time, if the selection I've made has these
illegal characters?

Here one example to work with:

Dim InvalidCharacters As Char() = IO.Path.GetInvalidFileNameChars
Dim StringToCheck As String = "C:\?Wrong.txt"
Dim IsOK As Boolean = True
For i As Integer = 0 To InvalidCharacters.Length - 1
If (StringToCheck.Contains(InvalidCharacters(i))) Then IsOK =
False : Exit For
Next
MsgBox(IsOK)

-Teemu
 
Seth --

Seth Williams said:
I'm trying to build a SAVE AS routine, like MSWord does -
I select and grab the first line of the document and Puts it in the SAVE
AS filename dialog

However, if there are illegal characters, this won't work correctly.

How can I check, ahead of time, if the selection I've made has these
illegal characters?

It doesn't make sense. There are many more restrictions than illegal
characters such as non-existing drives and directories, path segments which
are too long for the file system, etc.

I suggest to attempt to save the file, catch exceptions, and show the dialog
again.
 
Back
Top