Windows Dev Center

Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Path.ChangeExtension Method

Changes the extension of a path string.

[Visual Basic]
Public Shared Function ChangeExtension( _
   ByVal path As String, _
   ByVal extension As String _
) As String
[C#]
public static string ChangeExtension(
 string path,
 string extension
);
[C++]
public: static String* ChangeExtension(
 String* path,
 String* extension
);
[JScript]
public static function ChangeExtension(
   path : String,
 extension : String
) : String;

Parameters

path
The path information to modify. The path cannot contain any of the characters defined in InvalidPathChars.
extension
The new extension (with a leading period). Specify a null reference (Nothing in Visual Basic) to remove an existing extension from path.

Return Value

A string containing the modified path information.

On Windows-based desktop platforms, if path is a null reference (Nothing in Visual Basic) or an empty string (""), the path information is returned unmodified. If extension is a null reference (Nothing), the returned string contains the specified path with its extension removed. If path has no extension, and extension is not a null reference (Nothing), the returned path string contains extension appended to the end of path.

Exceptions

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

Remarks

If neither path nor extension contains a period (.), ChangeExtension adds the period.

The extension parameter can contain multiple periods and any valid path characters, and can be any length. If extension is a null reference (Nothing in Visual Basic), the returned string contains the contents of path with the last period and all characters following it removed.

If extension is an empty string, the returned path string contains the contents of path with any characters following the last period removed.

If path does not have an extension and extension is not a null reference (Nothing), the returned string contains path followed by extension.

If extension is not a null reference (Nothing) and does not contain a leading period, the period is added.

If path contains a multiple extension separated by multiple periods, the returned string contains the contents of path with the last period and all characters following it replaced by extension. For example, if path is "\Dir1\examples\pathtests.csx.txt" and extension is "cs", the modified path is "\Dir1\examples\pathtests.csx.cs".

It is not possible to verify that the returned results are valid in all scenarios. For example, if path is empty, extension is appended.

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
Retrieve a file extension. GetExtension
Retrieve the fully qualified path of a file. GetFullPath
Retrieve only the file name from a path. GetFileNameWithoutExtension
Retrieve only the directory name from a path. GetDirectoryName

Example

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

[Visual Basic] 
Imports System
Imports System.IO

Public Class PathSnippets
    Public Sub ChangeExtension()
        Dim goodFileName As String = "C:\mydir\myfile.com.extension"
        Dim badFileName As String = "C:\mydir\"
        Dim result As String
        result = Path.ChangeExtension(goodFileName, ".old")
        Console.WriteLine("ChangeExtension({0}, '.old') returns '{1}'", goodFileName, result)
        result = Path.ChangeExtension(goodFileName, "")
        Console.WriteLine("ChangeExtension({0}, '') returns '{1}'", goodFileName, result)
        result = Path.ChangeExtension(badFileName, ".old")
        Console.WriteLine("ChangeExtension({0}, '.old') returns '{1}'", badFileName, result)

[C#] 
using System;
using System.IO;

public class PathSnippets
{

    public void ChangeExtension()
    {
        string goodFileName = @"C:\mydir\myfile.com.extension";
        string badFileName = @"C:\mydir\";
        string result;

        result = Path.ChangeExtension(goodFileName, ".old");
        Console.WriteLine("ChangeExtension({0}, '.old') returns '{1}'",
            goodFileName, result); 

        result = Path.ChangeExtension(goodFileName, "");
        Console.WriteLine("ChangeExtension({0}, '') returns '{1}'", 
            goodFileName, result); 

        result = Path.ChangeExtension(badFileName, ".old");
        Console.WriteLine("ChangeExtension({0}, '.old') returns '{1}'", 
            badFileName, result); 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::IO;

void ChangeExtension()
{
   String * goodFileName = S"C:\\mydir\\myfile.com.extension";
   String * badFileName = S"C:\\mydir\\";
   String * result;

   result = Path::ChangeExtension(goodFileName, ".old");
   Console::WriteLine(S"ChangeExtension({0}, '.old') returns '{1}'",
      goodFileName, result); 

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

   result = Path::ChangeExtension(badFileName, ".old");
   Console::WriteLine(S"ChangeExtension({0}, '.old') returns '{1}'", 
      badFileName, 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:
© 2015 Microsoft