Path.GetFileNameWithoutExtension 方法

定義

多載

GetFileNameWithoutExtension(ReadOnlySpan<Char>)

傳回檔案路徑的檔案名稱和副檔名,此路徑以唯讀的字元範圍表示。

GetFileNameWithoutExtension(String)

傳回沒有副檔名的指定路徑字串的檔案名稱。

GetFileNameWithoutExtension(ReadOnlySpan<Char>)

來源:
Path.cs
來源:
Path.cs
來源:
Path.cs

傳回檔案路徑的檔案名稱和副檔名,此路徑以唯讀的字元範圍表示。

public:
 static ReadOnlySpan<char> GetFileNameWithoutExtension(ReadOnlySpan<char> path);
public static ReadOnlySpan<char> GetFileNameWithoutExtension (ReadOnlySpan<char> path);
static member GetFileNameWithoutExtension : ReadOnlySpan<char> -> ReadOnlySpan<char>
Public Shared Function GetFileNameWithoutExtension (path As ReadOnlySpan(Of Char)) As ReadOnlySpan(Of Char)

參數

path
ReadOnlySpan<Char>

包含路徑的唯讀範圍,可從該路徑取得沒有副檔名的檔案名稱。

傳回

GetFileName(ReadOnlySpan<Char>) 傳回的唯讀範圍字元,不含最後的句號 (.) 以及其後所有字元。

另請參閱

適用於

GetFileNameWithoutExtension(String)

來源:
Path.cs
來源:
Path.cs
來源:
Path.cs

傳回沒有副檔名的指定路徑字串的檔案名稱。

public:
 static System::String ^ GetFileNameWithoutExtension(System::String ^ path);
public static string GetFileNameWithoutExtension (string path);
public static string? GetFileNameWithoutExtension (string? path);
static member GetFileNameWithoutExtension : string -> string
Public Shared Function GetFileNameWithoutExtension (path As String) As String

參數

path
String

檔案的路徑。

傳回

GetFileName(ReadOnlySpan<Char>) 傳回的字串,但不包含最後的句號 (.) 以及其後的所有字元。

例外狀況

.NET Framework 和 2.1 之前的 .NET Core 版本:path包含中GetInvalidPathChars()定義的一或多個無效字元。

範例

下列範例示範 如何使用 GetFileNameWithoutExtension 方法。

String^ fileName = "C:\\mydir\\myfile.ext";
String^ path = "C:\\mydir\\";
String^ result;
result = Path::GetFileNameWithoutExtension( fileName );
Console::WriteLine( "GetFileNameWithoutExtension('{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:
//
// GetFileNameWithoutExtension('C:\mydir\myfile.ext') returns 'myfile'
// GetFileName('C:\mydir\') returns ''
string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string result;

result = Path.GetFileNameWithoutExtension(fileName);
Console.WriteLine("GetFileNameWithoutExtension('{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:
//
// GetFileNameWithoutExtension('C:\mydir\myfile.ext') returns 'myfile'
// GetFileName('C:\mydir\') returns ''
Dim fileName As String = "C:\mydir\myfile.ext"
Dim pathname As String = "C:\mydir\"
Dim result As String

result = Path.GetFileNameWithoutExtension(fileName)
Console.WriteLine("GetFileNameWithoutExtension('{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:
'
' GetFileNameWithoutExtension('C:\mydir\myfile.ext') returns 'myfile'
' GetFileName('C:\mydir\') returns ''

備註

這個方法不會驗證路徑或檔名是否存在。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於