Path.GetFileName メソッド
定義
オーバーロード
GetFileName(ReadOnlySpan<Char>) |
読み取り専用の文字範囲で表されたファイル パスのファイル名と拡張子を返します。Returns the file name and extension of a file path that is represented by a read-only character span. |
GetFileName(String) |
指定したパス文字列のファイル名と拡張子を返します。Returns the file name and extension of the specified path string. |
GetFileName(ReadOnlySpan<Char>)
読み取り専用の文字範囲で表されたファイル パスのファイル名と拡張子を返します。Returns the file name and extension of a file path that is represented by a read-only character span.
public:
static ReadOnlySpan<char> GetFileName(ReadOnlySpan<char> path);
public static ReadOnlySpan<char> GetFileName (ReadOnlySpan<char> path);
static member GetFileName : ReadOnlySpan<char> -> ReadOnlySpan<char>
Public Shared Function GetFileName (path As ReadOnlySpan(Of Char)) As ReadOnlySpan(Of Char)
パラメーター
- path
- ReadOnlySpan<Char>
ファイル名と拡張子の取得元のパスを含む読み取り専用の範囲。A read-only span that contains the path from which to obtain the file name and extension.
戻り値
path
の最後のディレクトリ区切り文字の後の文字。The characters after the last directory separator character in path
.
注釈
返される読み取り専用スパンには、の最後の区切り記号に続くパスの文字が含まれ path
ます。The returned read-only span contains the characters of the path that follow the last separator in path
. の最後の文字 path
がボリュームまたはディレクトリの区切り文字の場合、メソッドはを返し ReadOnlySpan<T>.Empty ます。If the last character in path
is a volume or directory separator character, the method returns ReadOnlySpan<T>.Empty. path
に区切り文字が含まれていない場合、メソッドはを返し path
ます。If path
contains no separator character, the method returns path
.
こちらもご覧ください
適用対象
GetFileName(String)
指定したパス文字列のファイル名と拡張子を返します。Returns the file name and extension of the specified path string.
public:
static System::String ^ GetFileName(System::String ^ path);
public static string GetFileName (string path);
static member GetFileName : string -> string
Public Shared Function GetFileName (path As String) As String
パラメーター
- path
- String
ファイル名と拡張子の取得元のパス文字列。The path string from which to obtain the file name and extension.
戻り値
path
の最後のディレクトリ区切り文字の後の文字。The characters after the last directory separator character in path
. path
の最後の文字がディレクトリ区切り記号またはボリューム区切り記号の場合、このメソッドは Empty を返します。If the last character of path
is a directory or volume separator character, this method returns Empty. path
が null
の場合、このメソッドは null
を返します。If path
is null
, this method returns null
.
例外
path
が、GetInvalidPathChars() で定義されている無効な文字を 1 つ以上含んでいます。path
contains one or more of the invalid characters defined in GetInvalidPathChars().
例
次の例は、Windows ベースのデスクトッププラットフォームでのメソッドの動作を示して GetFileName
います。The following example demonstrates the behavior of the GetFileName
method on a Windows-based desktop platform.
String^ fileName = "C:\\mydir\\myfile.ext";
String^ path = "C:\\mydir\\";
String^ result;
result = Path::GetFileName( fileName );
Console::WriteLine( "GetFileName('{0}') returns '{1}'", fileName, result );
result = Path::GetFileName( path );
Console::WriteLine( "GetFileName('{0}') returns '{1}'", path, result );
// This code produces output similar to the following:
//
// GetFileName('C:\mydir\myfile.ext') returns 'myfile.ext'
// GetFileName('C:\mydir\') returns ''
string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string result;
result = Path.GetFileName(fileName);
Console.WriteLine("GetFileName('{0}') returns '{1}'",
fileName, result);
result = Path.GetFileName(path);
Console.WriteLine("GetFileName('{0}') returns '{1}'",
path, result);
// This code produces output similar to the following:
//
// GetFileName('C:\mydir\myfile.ext') returns 'myfile.ext'
// GetFileName('C:\mydir\') returns ''
Dim fileName As String = "C:\mydir\myfile.ext"
Dim pathname As String = "C:\mydir\"
Dim result As String
result = Path.GetFileName(fileName)
Console.WriteLine("GetFileName('{0}') returns '{1}'", fileName, result)
result = Path.GetFileName(pathname)
Console.WriteLine("GetFileName('{0}') returns '{1}'", pathname, result)
' This code produces output similar to the following:
'
' GetFileName('C:\mydir\myfile.ext') returns 'myfile.ext'
' GetFileName('C:\mydir\') returns ''
注釈
ファイルパスがの場合、戻り値はです null
null
。The returned value is null
if the file path is null
.
ファイル名の先頭を決定するために使用される区切り文字は DirectorySeparatorChar 、と AltDirectorySeparatorChar です。The separator characters used to determine the start of the file name are DirectorySeparatorChar and AltDirectorySeparatorChar.
\ は Unix の有効なファイル名であるため、 GetFileName
unix ベースのプラットフォームで実行する場合、 C: \ Mydir \ myfile.txt などの windows ベースのパスからファイル名を正しく返すことはできませんが、 GetFileName
windows ベースのプラットフォームで実行していると、 /tmp/myfile.ext のような unix ベースのパスからファイル名を正しく返すことができ GetFileName
ます。Because \ is a legal file name on Unix, GetFileName
running under Unix-based platforms cannot correctly return the file name from a Windows-based path like C:\mydir\myfile.ext, but GetFileName
running under Windows-based platforms can correctly return the file name from a Unix-based path like /tmp/myfile.ext, so the behavior of the GetFileName
method is not strictly the same on Unix-based and Windows-based platforms.
共通 I/O タスクの一覧は、 共通 I/O タスク を参照してください。For a list of common I/O tasks, see Common I/O Tasks.
こちらもご覧ください
- Windows システムのファイル パス形式File path formats on Windows systems
- ファイルおよびストリーム入出力File and Stream I/O
- 方法:ファイルからのテキストの読み取りHow to: Read Text from a File
- 方法:ファイルにテキストを書き込むHow to: Write Text to a File