Path.GetFileNameWithoutExtension 方法
定义
重载
| GetFileNameWithoutExtension(ReadOnlySpan<Char>) |
返回文件名,该文件名不带由只读字符范围表示的文件路径扩展名。Returns the file name without the extension of a file path that is represented by a read-only character span. |
| GetFileNameWithoutExtension(String) |
返回不具有扩展名的指定路径字符串的文件名。Returns the file name of the specified path string without the extension. |
GetFileNameWithoutExtension(ReadOnlySpan<Char>)
返回文件名,该文件名不带由只读字符范围表示的文件路径扩展名。Returns the file name without the extension of a file path that is represented by a read-only character span.
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>
一个只读范围,包含从中获取没有扩展名的文件名的路径。A read-only span that contains the path from which to obtain the file name without the extension.
返回
由 GetFileName(ReadOnlySpan<Char>) 返回的只读范围中的字符,但不包括最后一个句点 (.) 及其后面的所有字符。The characters in the read-only span returned by GetFileName(ReadOnlySpan<Char>), minus the last period (.) and all characters following it.
适用于
GetFileNameWithoutExtension(String)
返回不具有扩展名的指定路径字符串的文件名。Returns the file name of the specified path string without the extension.
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
文件的路径。The path of the file.
返回
由 GetFileName(ReadOnlySpan<Char>) 返回的字符串,但不包括最后的句点 (.) 以及之后的所有字符。The string returned by GetFileName(ReadOnlySpan<Char>), minus the last period (.) and all characters following it.
例外
.NET Framework 和 .NET Core 版本早于2.1: path 包含中定义的一个或多个无效字符 GetInvalidPathChars() 。.NET Framework and .NET Core versions older than 2.1: path contains one or more of the invalid characters defined in GetInvalidPathChars().
示例
下面的示例演示方法的用法 GetFileNameWithoutExtension 。The following example demonstrates a use of the GetFileNameWithoutExtension method.
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 ''
注解
此方法不会验证路径或文件名是否存在。This method does not verify that the path or file name exists.
有关常见 i/o 任务的列表,请参阅 常见 I/o 任务。For a list of common I/O tasks, see Common I/O Tasks.