Path.GetInvalidFileNameChars Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Hämtar en matris som innehåller de tecken som inte tillåts i filnamn.
public:
static cli::array <char> ^ GetInvalidFileNameChars();
public static char[] GetInvalidFileNameChars();
static member GetInvalidFileNameChars : unit -> char[]
Public Shared Function GetInvalidFileNameChars () As Char()
Returer
En matris som innehåller de tecken som inte tillåts i filnamn.
Exempel
I följande exempel visas metoden GetInvalidFileNameChars och GetInvalidPathChars metoden för att hämta ogiltiga tecken.
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
// ...
Imports System.IO
Namespace PathExample
Public Class GetCharExample
Public Shared Sub Main()
' Get a list of invalid path characters.
Dim invalidPathChars() As Char = Path.GetInvalidPathChars()
Console.WriteLine("The following characters are invalid in a path:")
ShowChars(invalidPathChars)
Console.WriteLine()
' Get a list of invalid file characters.
Dim invalidFileChars() As Char = Path.GetInvalidFileNameChars()
Console.WriteLine("The following characters are invalid in a filename:")
ShowChars(invalidFileChars)
End Sub
Public Shared Sub ShowChars(charArray As Char())
Console.WriteLine("Char" + vbTab + "Hex Value")
' Display each invalid character to the console.
For Each someChar As Char In charArray
If Char.IsWhiteSpace(someChar)
Console.WriteLine("," + vbTab + "{0:X4}", _
Microsoft.VisualBasic.Asc(someChar))
Else
Console.WriteLine("{0:c}," + vbTab +"{1:X4}", someChar, _
Microsoft.VisualBasic.Asc(someChar))
End If
Next someChar
End Sub
End Class
End Namespace
' 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
' ...
Kommentarer
Matrisen som returneras från den här metoden är inte garanterad att innehålla den fullständiga uppsättningen tecken som är ogiltiga i fil- och katalognamn. Den fullständiga uppsättningen ogiltiga tecken kan variera beroende på filsystem. På Windows-baserade skrivbordsplattformar kan ogiltiga sökvägstecken till exempel innehålla ASCII/Unicode-tecken 1 till och med 31, samt citattecken ("), mindre än (<), större än (>), pipe (|), backspace (\b), null (\0) och flik (\t).