The Self-Taught Programmer

Recipes from real-world experience with asp.net.

Posts Tagged ‘URL

How to Get the Name of the Current Folder from the URL

leave a comment »

The Situation:

You need to get the name of the current page request folder from the URL.

A Solution:

Use this method:

public string GetCurrentFolderName()
{
	//get current file path 
	string folder = System.Web.HttpContext.Current.Request.FilePath;

	//find the position of the last '/' 
	int intPos = folder.LastIndexOf("/");

	// if it is not the root folder...
	if ( intPos > 0 )
	{
		//cut off all characters after the last '/' 
		folder = folder.Substring(1, intPos - 1);

		//find the position of the last '/' in the new string 
		int intPos2 = folder.LastIndexOf("/");

		//get the length of the characters between the two '/' 
		int intLength = ( intPos - 1 ) - ( intPos2 + 1 );

		//get the characters between the 2 '/' 
		folder = folder.Substring(intPos2 + 1, intLength);
	}
	else
	{
		folder = "";
	}

	return folder;
}
Advertisements

Written by Jim McMullen

December 2, 2011 at 11:06 am

Posted in How-To

Tagged with ,