Path: ChangeExtension |
Changes the extension of a path string.
Public Function ChangeExtension( ByRef Path As String, ByRef Extension As String ) As String
If Path has length zero, vbNullString is returned. If Extension is vbNullString, the returned string contains the specified path with its extension removed. If Path has no extension, and Extension is not vbNullString, the returned string contains Extension appended to the end of Path.
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 vbNullString, 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 vbNullString, the returned string contains Path followed by Extension.
If Extension is not vbNullString 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".
Exception | Condition |
---|---|
ArgumentException | Path contains one or more of the invalid characters defined in GetInvalidPathChars. |
The following code example demonstrates a use of the ChangeExtension method.
Public Sub Main() Const GoodFileName As String = "c:\MyDir\MyFile.Com.Extension" Const BadFileName As String = "c:\MyDir\" Dim Result As String Result = Path.ChangeExtension(GoodFileName, ".old") Debug.Print CorString.Format("ChangeExtension({0}, '.old') return '{1}'", GoodFileName, Result) Result = Path.ChangeExtension(GoodFileName, "") Debug.Print CorString.Format("ChangeExtension({0}, '') return '{1}')", GoodFileName, Result) Result = Path.ChangeExtension(BadFileName, ".old") Debug.Print CorString.Format("ChangeExtension({0}, '.old') returns '{1}'", BadFileName, Result) End Sub ' This example code produces the following output. ' ' ChangeExtension(c:\MyDir\MyFile.Com.Extension, '.old') return 'c:\MyDir\MyFile.Com.old' ' ChangeExtension(c:\MyDir\MyFile.Com.Extension, '') return 'c:\MyDir\MyFile.Com.') ' ChangeExtension(c:\MyDir\, '.old') returns 'c:\MyDir\.old'