views:

484

answers:

2

I'm in the midst of some refactoring some C# code and part of this is to redo some references, because we're redoing the folder structure entirely. What I'd like to do is just go into the .csproj or .sln file and modify the paths.

However some of the references have paths like

"../../../../../../ThirdParty/SomeMicrosoftLibrary/bin/Debug/SomeMicrosoftLibrary.dll

And since we've moved everything around I need to find the new relative path. But I absolutely hate trying to do this (figure out how many slashes and periods I need to put in) since it always feels like some hit or miss science.

Is there some easy way (utility, script, snippet of code) to say "here's file A, here's file B, what is the relative path of file B in relation to file A?"

+1  A: 

With Ruby:

$ ruby -e "require 'pathname'; puts Pathname.new('foo/bar').relative_path_from(Pathname.new('baz/x/y/z')).to_s"
../../../../foo/bar

I'm pretty sure Python has a similar method, though I don't have it handy.

I don't think there's such a method in Python's standard library, but a Google search turns up plenty of examples.
David Zaslavsky
*checks* Isn't it just os.path.relpath(a, b)?
+1  A: 

Generate a Relative Path

It's in Java, but easily translatable to C#.

Chris Doggett