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

[characters that are invalid]

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

private void Form1_Load(object sender, EventArgs e)
{
    string txt = "";
    foreach (char ch in Path.GetInvalidFileNameChars())
    {
        if (Char.IsWhiteSpace(ch) || Char.IsControl(ch))
            txt += "<" + (int)ch + "> ";
        else
            txt += ch + " ";
    }
    txtInvalidFileNameChars.Text = txt;

    txt = "";
    foreach (char ch in Path.GetInvalidPathChars())
    {
        if (Char.IsWhiteSpace(ch) || Char.IsControl(ch))
            txt += "<" + (int)ch + "> ";
        else
            txt += ch + " ";
    }
    txtInvalidPathChars.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.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in files, strings and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *