Jaydeep
Jaydeep

Reputation: 305

Path.GetDirectoryName in C# .net core

I am having a test case in .Net Core 2 where I am providing below input to Path.GetDirectoryName.

@"C:" + Path.DirectorySeparatorChar

When I assert for Path.GetDirectoryNamein my test case, it returns nullfor Windows OS but returns C:for Linux OS, why so? Can anyone please explain what wrong I am doing here?

Upvotes: 0

Views: 1440

Answers (1)

Charlie
Charlie

Reputation: 13736

On Windows, C:\ is a root directory. It doesn't have a name.

On Linux, C:\ is also a directory. It's name is c: and - since you used a relative path - it's a sub-directory of the current directory.

The problem is that you have correctly used Path.DirectorySeparatorChar so that the provided path works in both operating systems, but you have used a drive designator (C:), which is something that doesn't exist in Linux.

Upvotes: 1

Related Questions