Monday, December 10, 2007

Invalid Characters

 

System.IO.Path class has two interesting (among others) static methods:

GetInvalidPathChars - Gets an (char[]) array containing the characters that are not allowed in path names.
GetInvalidFileNameChars - Gets an (char[]) array containing the characters that are not allowed in file and directory names.

 

The methods name are pretty straightforward, lets examine the results from both methods, forcing the output to a one line string:

PS > $GetInvalidPathChars = [System.IO.Path]::GetInvalidPathChars()
PS > $GetInvalidFileNameChars = [System.IO.Path]::GetInvalidFileNameChars()

Clipboard01


 

 

 

 

 

 

Although I forced the output to a string, you can see that the result shows in two lines. This is because there is a rendered non printable char (ENTER).

Now, print the characters ASCII value:

PS > "$($GetInvalidPathChars | foreach { [int]$_ } )"
34 60 62 124 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

PS > "$($GetInvalidFileNameChars | foreach { [int]$_} )"
34 60 62 124 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 58 42 63 92 47

PowerShell utilizes GetInvalidPathChars() method in the Test-Path -IsValid parameter.

Note that the above list of invalid characters can vary based on the system you're running the code on.

No comments: