System.IO.Path.GetInvalidPathChars 方法

方法描述

获取包含不允许在路径名中使用的字符的数组。

语法定义(C# System.IO.Path.GetInvalidPathChars 方法 的用法)

public static char[] GetInvalidPathChars()

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
返回值 System.Char[] 包含不允许在路径名中使用的字符的数组。

提示和注释

不保证通过此方法返回的数组包含文件和目录名中无效的完整字符集。 无效字符的完整字符集可能因文件系统而异。 例如,在基于 Windows 的桌面平台上,无效路径字符可能包括从 1 到 31 的 ASCII/Unicode 字符,以及引号 (")、小于号 (<)、大于号 (>)、管道符号 (|)、退格 (\b)、null (\0) 和制表符 (\t)。

System.IO.Path.GetInvalidPathChars 方法例子

下面的代码示例检索无效字符的 GetInvalidFileNameChars 和 GetInvalidPathChars。

using System;
using System.IO;

namespace PathExample
{
    class GetCharExample
    {
        public static void Main()
        {
            // Get a list of invalid path characters.
            char[] invalidPathChars = Path.GetInvalidPathChars();

            Console.WriteLine("The following characters are invalid in a path:");
            ShowChars(invalidPathChars);
            Console.WriteLine();

            // Get a list of invalid file characters.
            char[] invalidFileChars = Path.GetInvalidFileNameChars();

            Console.WriteLine("The following characters are invalid in a filename:");
            ShowChars(invalidFileChars);
        }

        public static void ShowChars(char[] charArray)
        {
            Console.WriteLine("Char\tHex Value");
            // Display each invalid character to the console.
            foreach (char someChar in charArray)
            {
                if (Char.IsWhiteSpace(someChar))
                {
                    Console.WriteLine(",\t{0:X4}", (int)someChar);
                }
                else
                {
                    Console.WriteLine("{0:c},\t{1:X4}", someChar, (int)someChar);
                }
            }
        }
    }
}
// Note: Some characters may not be displayable on the console.
// The output will look something like:
//
// The following characters are invalid in a path:
// Char    Hex Value
// ",      0022
// <,      003C
// >,      003E
// |,      007C
// ...
//
// The following characters are invalid in a filename:
// Char    Hex Value
// ",      0022
// <,      003C
// >,      003E
// |,      007C
// ...

异常

异常 异常描述

命名空间

namespace: System.IO

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。