Export (0) Print
Expand All

Path.GetFileNameWithoutExtension Method

Returns the file name of the specified path string without the extension.

[Visual Basic]
Public Shared Function GetFileNameWithoutExtension( _
   ByVal path As String _
) As String
[C#]
public static string GetFileNameWithoutExtension(
 string path
);
[C++]
public: static String* GetFileNameWithoutExtension(
 String* path
);
[JScript]
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.

Exceptions

Exception Type Condition
ArgumentException path contains one or more of the invalid characters defined in InvalidPathChars.

Remarks

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. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending to a Log File

File.AppendText

FileInfo.AppendText

Retrieve a file extension. GetExtension
Retrieve the fully qualified path of a file. GetFullPath
Retrieve the file name and extension from a path. GetFileName
Retrieve only the directory name from a path. GetDirectoryName
Change the extension of a file. ChangeExtension

Example

[Visual Basic, C#, C++] The following example demonstrates a use of the GetFileNameWithoutExtension method.

[Visual Basic] 
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("GetFileNameWithoutExtension('{0}') returns '{1}'", pathname, result)

[C#] 
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("GetFileNameWithoutExtension('{0}') returns '{1}'", 
    path, result);

[C++] 
String * fileName = S"C:\\mydir\\myfile.ext";
String * path = S"C:\\mydir\\";
String * result;

result = Path::GetFileNameWithoutExtension(fileName);
Console::WriteLine(S"GetFileNameWithoutExtension('{0}') returns '{1}'", 
   fileName, result);

result = Path::GetFileName(path);
Console::WriteLine(S"GetFileNameWithoutExtension('{0}') returns '{1}'", 
   path, result);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Path Class | Path Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2014 Microsoft