Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uri.MakeRelativeUri incorrectly handling ../

I'm getting an incorrect path when using MakeRelativeUri. Now this could be an issue with Unity and mono or hopefully I'm just using it wrong or maybe there's actually a bug in .net. You'll have to forgive the elaborate example but this is what I have.

System.Uri fileURI = new System.Uri( "/Home/Folder1/Folder2/Folder3/Folder4/Folder5/Folder6/../../../../Folder7/Folder8/file.png" );
System.Uri rootURI = new System.Uri( "/Home/Folder1/Folder2/Folder3/../FolderA/." );
result = rootURI.MakeRelativeUri( fileURI ).ToString();

result in this case is ../../Folder4/Folder5/Folder6/../../../../Folder7/Folder8/file.png

but it should be ../Folder3/Folder4/Folder5/Folder6/../../../../Folder7/Folder8/file.png

It seems as though it is treating the ../ in the root path as a directory rather than an instruction to go back a directory. Can anyone confirm if thats the case?

like image 808
Real World Avatar asked Nov 23 '25 18:11

Real World


1 Answers

I've worked around this by passing the path strings through Path.GetFullPath() before passing them into the Uri constructor. That strips out the ../ correctly and this then works as you would expect.

like image 79
Real World Avatar answered Nov 26 '25 16:11

Real World



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!