Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get a relative path

Status
Not open for further replies.

tillhm

Programmer
Jan 6, 2006
3
DE
Hello,

I'm searching for a function that returns the relative path of two absolute paths passed as arguments.
E.g. I'd like to get ..\myfile.txt if the absolute paths are
C:\Program Files and
C:\myfile.txt

I'd like to know whether there is a function to merge a relative and an absolute path as well.

Till

Please excuse my bad English.
 
If I understand correctly, you just want to extract either the folder name or the file name:

C:\Program Files -> "Program Files"
C:\myfile.txt -> "myfile.txt"

and ignore the rest of the path?

Code:
string path = "C:\Program Files";

int index = path.LastIndexOf("\\");
index++;

string name = path.SubString(index, path.Length - index);

name should now = "Program Files"


Is that what you were looking for?




 
No, that's not what I mean.

Extracting the folder name can be done by using System.IO.Path.GetDirectoryName(mystring);

I want to get the relative path to myFile.txt if I am in the folder C:\Program Files. The relative path is ..\myFile.txt
.. = go up one folder
myFile.txt = the file

Another example
Folder = C:\Program Files
File = C:\Program Files\Visual Studio\devenv.exe
Relative Path = Visual Studio\devenv.exe
 
a hack

string[] s_array = Regex.Split(_input,@"\\");
Console.WriteLine(s_array[s_array.Length-2] + "\\" + s_array[s_array.Length-1]);

Marty
 
I wrote this code on my own and if someone thinks its usefull, fell free to use it.

Code:
namespace System.IO
{
	public sealed class PathEx
	{
		public static string GetRelativePath(string sFolder, string sItem)
		{
			//check whether the files are on the same drive
			if (sFolder.ToUpper()[0] != sItem.ToUpper()[0])
				return sItem; //there is no relative path if the item is on another drive
			//split both paths
			System.Collections.ArrayList strFolderArray = 
				new System.Collections.ArrayList(sFolder.Split("\\".ToCharArray()));
			System.Collections.ArrayList strItemArray = 
				new System.Collections.ArrayList(sItem.Split("\\".ToCharArray()));
			//compare the paths
			while(strFolderArray.Count != 0 && strItemArray != 0)
			{
				//not case sensitive
				if(((string)strFolderArray[0]).ToUpper() == ((string)strItemArray[0]).ToUpper())
				{
					strFolderArray.RemoveAt(0);
					strItemArray.RemoveAt(0);
				}
				else
					break; //path is no longer equal
			}

			//Add .. as often as strFolderArray.Count
			for (int i = 0; i < strFolderArray.Count; i++)
				strItemArray.Insert(0, "..");

			//merge the string contained in strItemArray
			string ret = "";
			foreach(string str in strItemArray)
				ret+=str+"\\";

			//return \\ at the end of the string (cause myfile.txt\ is not valid)
			ret = ret.Substring(0, ret.Length-1);

			return ret;
		}

		public static string MergePaths(string sFolder, string sRelative)
		{
			//split both paths
			System.Collections.ArrayList strFolderArray = 
				new System.Collections.ArrayList(sFolder.Split("\\".ToCharArray()));
			System.Collections.ArrayList strRelativeArray = 
				new System.Collections.ArrayList(sRelative.Split("\\".ToCharArray()));

			//delete \\something\\ as often as ..\\ is in front of sRelative
			foreach(string str in strRelativeArray)
			{
				if(str == "..")
				{
					//delete itself
					strRelativeArray.Remove(str);
					//delete the folder
					strFolderArray.RemoveAt(strFolderArray.Count-1);
				}
			}

			//merge the paths
			string ret = "";
			foreach(string str in strFolderArray)
				ret+=str+"\\";
			foreach(string str in strRelativeArray)
				ret+=str+"\\";

			//return \\ at the end of the string (cause myfile.txt\ is not valid)
			ret = ret.Substring(0, ret.Length-1);

			return ret;
		}
	}
}
 
tillhm,

Will that code work for this purpose:
I have a folder named inventory that contains a database and another folder named images.

On my form I'm trying to have a button that lauches the picker, where I select the image for the inventory item.
Once selected, it passes the relative path to a text box, "ImgPath".

(That way if you have to download the database to CD/DVD or move it to a different computer, you don't lose all the image paths.)

Let me know what you think.
-Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top