Export (0) Print
Expand All

Path.ChangeExtension Method

Changes the extension of a path string.

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

public static string ChangeExtension (
	string path,
	string extension
)
public static String ChangeExtension (
	String path, 
	String extension
)
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 in Visual Basic), the returned string contains the specified path with its extension removed. If path has no extension, and extension is not a null reference (Nothing in Visual Basic), the returned path string contains extension appended to the end of path.

Exception typeCondition

ArgumentException

The path contains one or more of the invalid characters defined in InvalidPathChars, or contains a wildcard character.

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

If extension is not a 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 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

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

The following code example demonstrates a use of the ChangeExtension method.

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'

import System.*;
import 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);

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 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