Rediger

Del via


Path.ChangeExtension(String, String) Method

Definition

Changes the extension of a path string.

public:
 static System::String ^ ChangeExtension(System::String ^ path, System::String ^ extension);
public static string ChangeExtension (string path, string extension);
public static string? ChangeExtension (string? path, string? extension);
static member ChangeExtension : string * string -> string
Public Shared Function ChangeExtension (path As String, extension As String) As String

Parameters

path
String

The path information to modify.

extension
String

The new extension (with or without a leading period). Specify null to remove an existing extension from path.

Returns

The modified path information.

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

Exceptions

.NET Framework and .NET Core versions older than 2.1: path contains one or more of the invalid characters defined in GetInvalidPathChars().

Examples

The following example demonstrates a use of the ChangeExtension method.

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

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 null, 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 null, the returned string contains path followed by extension.

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

Applies to

See also