HOW TO GET THE FOLDER PATH WHERE MY CURRENT .aspx FILE RESIDES IN VB.Net

I came across a situation , where I had multiple folders in my application (Lottery,Single DOnation ,Multiple Donation).Each of these folders contained multiple .aspx files with same name with hierarchy as shown below

Donation

-Single Donation

>>Index.aspx

>>details.aspx

>>confirm.aspx

-Multiple Donation

>>Index.aspx

>>details.aspx

>>confirm.aspx

-Lottery

>>Index.aspx

>>details.aspx

>>confirm.aspx

The logic was written so that based on the application path, the .aspx pages relevant to each folder were displayed .Although much of the content (server control, user control etc.) in most of these pages were identical , there were scenarios where I had to enable/disable a control based on the root folder .For instance , if it is the index.aspx and I am displaying the index page under Single Donation ,I do not want to display the mysampletextbox.text .

The trick here is to strip out the immediate parent folder from TemplateSourceDirectory property of an aspx page. This can be done in 3 simple lines of code

‘gives you the directory path for your current page i.e “/Application/Donations/Lottery” As string in my case

Dim myTempSourceDir As String = Page.TemplateSourceDirectory

‘Following 2 lines of code is a simple logic to Strip off the “Lottery” part from the above string

Dim myTrimIndex As Integer = myTempSourceDir.LastIndexOf(“/”)

Dim myRootFolder As String = myTempSourceDir.Remove(0, 24)

If myRootFolder = (“Lottery”) Then

mysampletextbox.Visible = True

Else

mysampletextbox.Visible = False

End If

Happy Coding 🙂 !!!!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s