Claudiu Constantin
Claudiu Constantin

Reputation: 2228

Why Path.GetDirectoryName() removes the trailing dot in directory name?

Path.GetDirectoryName("/folder/subfolder./file.txt");

I expected the result to be \folder\subfolder., but it is \folder\subfolder (without the dot). Why is the trailing dot removed?

Edit: it seems that you're not allowed to create such a folder in Windows. But take a look at this fiddle, it seems that in .net 4.5 the dot is included? https://dotnetfiddle.net/mq0OhR

Upvotes: 2

Views: 1035

Answers (3)

SSD
SSD

Reputation: 1391

In windows, it is not allowed to have period (.) as last character of directory name.

MSDN says

Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not. However, it is acceptable to specify a period as the first character of a name. For example, ".temp".

So .net assumes this behavior and removes it by default

Upvotes: 0

Daniel Rafael Wosch
Daniel Rafael Wosch

Reputation: 1094

Referring to https://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx Microsoft says the following in the Naming Conventions part:

Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not. However, it is acceptable to specify a period as the first character of a name. For example, ".temp".

Maybe thats the cause / reason the dot gets removed.

Upvotes: 3

Simply Ged
Simply Ged

Reputation: 8672

Are you using .NET 4.6.1 or earlier? This was changed and works correctly in 4.7 and later

Upvotes: 1

Related Questions