Path.ChangeExtension(String, String) 方法

定义

更改路径字符串的扩展名。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

参数

path
String

要修改的路径信息。The path information to modify.

extension
String

新的扩展名(有或没有前导句点)。The new extension (with or without a leading period). 指定 null 以从 path 移除现有扩展名。Specify null to remove an existing extension from path.

返回

String

已修改的路径信息。The modified path information.

在基于 Windows 的桌面平台上,如果 pathnull 或空字符串 (“”),则返回的路径信息是未修改的。On Windows-based desktop platforms, if path is null or an empty string (""), the path information is returned unmodified. 如果 extensionnull,则返回的字符串包含指定的路径(其扩展名已移除)。If extension is null, the returned string contains the specified path with its extension removed. 如果 path 不具有扩展名且 extension 不为 null,则返回的路径字符串包含追加到 path 结尾的 extensionIf path has no extension, and extension is not null, the returned path string contains extension appended to the end of path.

例外

.NET Framework 和 .NET Core 版本早于2.1: path 包含中定义的一个或多个无效字符 GetInvalidPathChars().NET Framework and .NET Core versions older than 2.1: path contains one or more of the invalid characters defined in GetInvalidPathChars().

示例

下面的示例演示方法的用法 ChangeExtensionThe 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'

注解

如果和都不 path extension 包含句点 ( ) , ChangeExtension 则添加句点。If neither path nor extension contains a period (.), ChangeExtension adds the period.

extension参数可包含多个句点和任意有效的路径字符,可以为任意长度。The extension parameter can contain multiple periods and any valid path characters, and can be any length. 如果 extensionnull ,则返回的字符串将包含的内容,其中包含 path 上一个句点以及删除后的所有字符。If extension is null, the returned string contains the contents of path with the last period and all characters following it removed.

如果 extension 是一个空字符串,则返回的路径字符串包含的内容, path 其中包含上一个句点之后的任何字符。If extension is an empty string, the returned path string contains the contents of path with any characters following the last period removed.

如果不 path 具有扩展名且不 extension 为,则 null 返回的字符串 path 后跟 extensionIf path does not have an extension and extension is not null, the returned string contains path followed by extension.

如果不 extensionnull 并且不包含前导句点,则添加句点。If extension is not null and does not contain a leading period, the period is added.

如果 path 包含由多个句点分隔的多个扩展,则返回的字符串将包含的内容, path 其最后一个句点和后的所有字符都将替换为 extensionIf 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. 例如,如果 path 是 "\Dir1\examples\pathtests.csx.txt",并且 extension 是 "cs",则修改后的路径为 "\Dir1\examples\pathtests.csx.cs"。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. 例如,如果 path 为空, extension 则追加。For example, if path is empty, extension is appended.

有关常见 i/o 任务的列表,请参阅 常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

适用于