Get full path with lower case file extension

S-Soft 666 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.

Developer technologies VB
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.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 60,161 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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.