共用方式為


Path.ChangeExtension Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

When it is called by trusted applications, changes the extension of a path string.

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

Syntax

'Declaration
Public Shared Function ChangeExtension ( _
    path As String, _
    extension As String _
) As String
public static string ChangeExtension(
    string path,
    string extension
)

Parameters

  • extension
    Type: System.String
    The new extension (with a leading period). Specify nulla 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 nulla null reference (Nothing in Visual Basic) or an empty string (""), the path information is returned unmodified. If extension is nulla 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 nulla null reference (Nothing in Visual Basic), the returned path string contains extension appended to the end of path.

Exceptions

Exception Condition
ArgumentException

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

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

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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference

Other Resources