HimDek
HimDek

Reputation: 147

Weird behaviour by System.IO.Path.Combine()

Console.WriteLine(System.IO.Path.Combine("C:\\some\\path\\", "this\\folder\\")); when run in microsoft's try-dotnet page, returns C:\some\path\/this\folder\.

I expect it to return C:\some\path\this\folder\.

How to fix this?

EDIT: In dotnetfiddle.net, however, I get the expected result. But I am worried because the official try-dotnet returns unexpected result.

Upvotes: 1

Views: 530

Answers (1)

Venkata N Bhupathi
Venkata N Bhupathi

Reputation: 417

This is an example from https://learn.microsoft.com/en-us/dotnet/api/system.io.path.combine?view=net-5.0

The problem with try-dotnet is that it is Unix-based system.

string[] paths = {@"d:\archives", "2001", "media", "images"};
string fullPath = Path.Combine(paths);
Console.WriteLine(fullPath);            

paths = new string[] {@"d:\archives\", @"2001\", "media", "images"};
fullPath = Path.Combine(paths);
Console.WriteLine(fullPath); 

paths = new string[] {"d:/archives/", "2001/", "media", "images"};
fullPath = Path.Combine(paths);
Console.WriteLine(fullPath); 
// The example displays the following output if run on a Windows system:
//    d:\archives\2001\media\images
//    d:\archives\2001\media\images
//    d:/archives/2001/media\images
//
// The example displays the following output if run on a Unix-based system:
//    d:\archives/2001/media/images
//    d:\archives\/2001\/media/images
//    d:/archives/2001/media/images

Upvotes: 4

Related Questions