Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the relative filename to a specific directory?

I have a method which is doing a file copy. The 'root' is specified by the user on the command line, which I sanitize with Path.GetFullPath(input).

I need to get the path of the file relative to that root, so the following cases would return:

Root        FilePath                    Return
y:\         y:\temp\filename1.txt       temp\filename1.txt
y:\dir1     y:\dir1\dir2\filename2.txt  dir2\filename2.txt
like image 449
esac Avatar asked Feb 24 '23 12:02

esac


1 Answers

You can write

var relative = new Uri(rootPath).MakeRelativeUri(new Uri(filePath));

http://msdn.microsoft.com/en-us/library/system.uri.makerelativeuri.aspx

like image 82
SLaks Avatar answered Mar 03 '23 21:03

SLaks