List characters that are illegal in file and path names in C#

This example uses the following code to display characters that are not allowed in file and path names.

string txt = "";
foreach (char ch in Path.GetInvalidFileNameChars())
{
if (Char.IsWhiteSpace(ch) || Char.IsControl(ch))
{
txt += "<" + (int)ch + "> ";
}
else
{
txt += ch + " ";
}
}
lblInvalidFileNameChars.Text = txt;

txt = "";
foreach (char ch in Path.GetInvalidPathChars())
{
if (Char.IsWhiteSpace(ch) || Char.IsControl(ch))
{
txt += "<" + (int)ch + "> ";
}
else
{
txt += ch + " ";
}
}
lblInvalidPathChars.Text = txt;

The code simply loops through the values returned by Path.GetInvalidFileNameChars and Path.GetInvalidPathChars. It displays the printable characters and shows the numeric values of the whitespace and control characters.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.