Export (0) Print
Expand All

Path.GetFileNameWithoutExtension Method

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

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

public static string GetFileNameWithoutExtension (
	string 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 typeCondition

ArgumentException

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.

How to: Write Text to a File

Write to a text file.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

Append text to a file.

How to: Open and Append 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

The following code 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);

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.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft