Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System.IO Namespace
Path Class
Path Methods
 ChangeExtension Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Path..::.ChangeExtension Method

Changes the extension of a path string.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Shared Function ChangeExtension ( _
    path As String, _
    extension As String _
) As String
Visual Basic (Usage)
Dim path As String
Dim extension As String
Dim returnValue As String

returnValue = Path.ChangeExtension(path, _
    extension)
C#
public static string ChangeExtension(
    string path,
    string extension
)
Visual C++
public:
static String^ ChangeExtension(
    String^ path, 
    String^ extension
)
JScript
public static function ChangeExtension(
    path : String, 
    extension : String
) : String

Parameters

path
Type: System..::.String
The path information to modify. The path cannot contain any of the characters defined in GetInvalidPathChars.
extension
Type: System..::.String
The new extension (with or without a leading period). Specify nullNothingnullptra null reference (Nothing in Visual Basic) to remove an existing extension from path.

Return Value

Type: System..::.String
A string containing the modified path information.
On Windows-based desktop platforms, if path is nullNothingnullptra null reference (Nothing in Visual Basic) or an empty string (""), the path information is returned unmodified. If extension is nullNothingnullptra null reference (Nothing in Visual Basic), the returned string contains the specified path with its extension removed. If path has no extension, and extension is not nullNothingnullptra null reference (Nothing in Visual Basic), the returned path string contains extension appended to the end of path.
ExceptionCondition
ArgumentException

path contains one or more of the invalid characters defined in GetInvalidPathChars.

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 nullNothingnullptra 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 nullNothingnullptra null reference (Nothing in Visual Basic), the returned string contains path followed by extension.

If extension is not nullNothingnullptra null reference (Nothing in Visual Basic) 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 a list of common I/O tasks, see Common I/O Tasks.

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

        ' This code produces output similar to the following:
        '
        ' ChangeExtension(C:\mydir\myfile.com.extension, '.old') returns 'C:\mydir\myfile.com.old'
        ' ChangeExtension(C:\mydir\myfile.com.extension, '') returns 'C:\mydir\myfile.com.'
        ' ChangeExtension(C:\mydir\, '.old') returns 'C:\mydir\.old'
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); 

        // This code produces output similar to the following:
        //
        // ChangeExtension(C:\mydir\myfile.com.extension, '.old') returns 'C:\mydir\myfile.com.old'
        // ChangeExtension(C:\mydir\myfile.com.extension, '') returns 'C:\mydir\myfile.com.'
        // ChangeExtension(C:\mydir\, '.old') returns 'C:\mydir\.old'
Visual C++
#using <system.dll>

using namespace System;
using namespace System::IO;
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 );

   // This code produces output similar to the following:
   //
   // ChangeExtension(C:\mydir\myfile.com.extension, '.old') returns 'C:\mydir\myfile.com.old'
   // ChangeExtension(C:\mydir\myfile.com.extension, '') returns 'C:\mydir\myfile.com.'
   // ChangeExtension(C:\mydir\, '.old') returns 'C:\mydir\.old'
JScript
var goodFileName : String = "C:\\mydir\\myfile.com.extension";
var badFileName : String = "C:\\mydir\\";
var result : 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); 

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Path methods affect strings, not files      KitWest   |   Edit   |   Show History
It is probably obvious to most readers that Path.ChangeExtension will not affect a file, only a string used to access a file.
Tags What's this?: Add a tag
Flag as ContentBug
ChangeExtension using PowerShell      Thomas Lee   |   Edit   |   Show History
# change-extensions.ps1
# changes extensions using PowerShell
# Thomas Lee - tfl@psp.co.uk

# setup file names
$goodFileName = "C:\mydir\myfile.com.extension"
$badFileName = "C:\mydir\"

# change file name
$result = [System.IO.Path]::ChangeExtension($goodFileName, ".old")
"ChangeExtension({0},'.old') returns '{1}'" -f $goodFileName, $result

$result = [System.IO.Path]::ChangeExtension($goodFileName, "")
"ChangeExtension({0}, '') returns '{1}'" -f $goodFileName, $result

$result = [System.IO.Path]::ChangeExtension($badFileName, ".old")
"ChangeExtension({0}, '.old') returns '{1}'" -f $badFileName, $result

This script produces the following output:

PS C:\Users\tfl> .\change-extensions.ps1
ChangeExtension(C:\mydir\myfile.com.extension,'.old') returns 'C:\mydir\myfile.com.old'
ChangeExtension(C:\mydir\myfile.com.extension, '') returns 'C:\mydir\myfile.com.'
ChangeExtension(C:\mydir\, '.old') returns 'C:\mydir\.old'


Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker