Path.GetFileNameWithoutExtension Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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. |
- Source:
- Path.cs
- Source:
- Path.cs
- Source:
- Path.cs
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)
Parameters
- path
- ReadOnlySpan<Char>
A read-only span that contains the path from which to obtain the file name without the extension.
Returns
The characters in the read-only span returned by GetFileName(ReadOnlySpan<Char>), minus the last period (.) and all characters following it.
See also
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Standard | 2.1 |
- Source:
- Path.cs
- Source:
- Path.cs
- Source:
- Path.cs
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
Parameters
- path
- String
The path of the file.
Returns
The string returned by GetFileName(ReadOnlySpan<Char>), minus the last period (.) and all characters following it.
Exceptions
.NET Framework and .NET Core versions older than 2.1: path
contains one or more of the invalid characters defined in GetInvalidPathChars().
Examples
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 ''
Remarks
This method does not verify that the path or file name exists.
For a list of common I/O tasks, see Common I/O Tasks.
See also
- 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
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: