Path.GetFileNameWithoutExtension Method
Assembly: mscorlib (in mscorlib.dll)
'Declaration Public Shared Function GetFileNameWithoutExtension ( _ path As String _ ) As String 'Usage Dim path As String Dim returnValue As String returnValue = Path.GetFileNameWithoutExtension(path)
public static String GetFileNameWithoutExtension ( String path )
public static function GetFileNameWithoutExtension ( path : String ) : String
Parameters
- path
The path of the file.
Return Value
A String containing the string returned by GetFileName, minus the last period (.) and all characters following it.| Exception type | Condition |
|---|---|
| path contains one or more of the invalid characters defined in InvalidPathChars, or contains a wildcard character. |
This method does not verify that the path or file name exists.
For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.
| To do this... | See the example in this topic... |
|---|---|
| Create a text file. | |
| Write to a text file. | |
| Read from a text file. | |
| Append text to a file. | |
| Retrieve a file extension. | |
| Retrieve the fully qualified path of a file. | |
| Retrieve the file name and extension from a path. | GetFileName |
| Retrieve only the directory name from a path. | |
| Change the extension of a file. |
The following code example demonstrates a use of the GetFileNameWithoutExtension method.
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 ''
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);
var fileName : String = "C:\\mydir\\myfile.ext"; var path : String = "C:\\mydir\\"; var result : String; result = Path.GetFileNameWithoutExtension(fileName); Console.WriteLine("GetFileNameWithoutExtension('{0}') returns '{1}'", fileName, result); result = Path.GetFileName(path); Console.WriteLine("GetFileName('{0}') returns '{1}'", path, result);
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.