Get full path with lower case file extension

S-Soft 646 Reputation points
2022-11-15T16:48:48.333+00:00

Hello
I have this string path:
C:\Users\Admin\Downloads\AbCd.PdF
Want to have:
C:\Users\Admin\Downloads\AbCd.pdf
Only the extension part needs to be converted to lower case, so:

Path.Combine(Path.GetDirectoryName(input), Path.GetFileNameWithoutExtension(input), Path.GetExtension(input).ToLowerInvariant)

And I get:
C:\Users\Admin\Downloads\AbCd.pdf

What's the story behind that backslash before extension?
And what's the safest culture invariant way to do this?
The path or file name might have non-ASCII characters.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,621 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,660 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 114.5K Reputation points
    2022-11-15T17:22:12.193+00:00

    I think that you should write something like this (in VB):

    Path.ChangeExtension(input, Path.GetExtension(input).ToLowerInvariant)

    or

    Path.Combine(Path.GetDirectoryName(input), Path.GetFileNameWithoutExtension(input) & Path.GetExtension(input).ToLowerInvariant)

    Path.Combine concatenates path elements (e.g. folder names) using '\' if required. It does not interprete the last argument as extension; it believes it is a folder.


1 additional answer

Sort by: Most helpful
  1. Michael Taylor 51,016 Reputation points
    2022-11-15T17:06:52.053+00:00

    What backslash before the extension? When you call Path.GetExtension it'll return .PDF. If you then call ToLower or ToLowerInvariant then it returns .pdf. If you want to take the current culture into account when lowercasing then use ToLower. If you are normalizing the value (like we often do with paths and keysthen useToLowerInvariant`.

    0 comments No comments