Developing Matt

My Technical Journal

Directory Path (where am I?)

leave a comment »

What if you want to access the current directory that the application is running in? If you are debugging it actually compiles and runs it in a temp directory and you get weird results when trying to access the stuff you normally access when running it as a strait exe. (try using streamwriter for a file named “test.txt” and you’ll have a fun time finding the file) All i want is to access the file in my debug folder. This is a pretty simple thing to do, but occasionally I lose my marbles and spend 10 valuable minutes trying to remember how to do it. I figure if I post it here I’ll look and feel smarter next time.

My.Application.Info.DirectoryPath
System.IO.Directory.GetCurrentDirectory
Appdomain.CurrentDomain.BaseDirectory
System.IO.Path.GetDirectoryName(system.Reflection.Assembly.GetExecutingAssembly.Location
Environment.CurrentDirectory (be careful with that one as current directory can be changed)
Curdir (same as above)
Application.StartupPath doesn’t work for me (M$ states that it only works if your library is in the same application path)

Path mapping in asp.net is a little different. Here are some methods from this site:

<%= Server.MapPath(Request.ServerVariables(“PATH_INFO” ))%><BR>
The preceding script produces the following output:
c:\inetpub\wwwroot\script\test.asp<BR>

<%= Server.MapPath(“data.txt”)%><BR>
<%= Server.MapPath(“script/data.txt”)%><BR>
The preceding scripts produce the following output:
c:\inetpub\wwwroot\script\data.txt<BR>
c:\inetpub\wwwroot\script\script\data.txt<BR>

<%= Server.MapPath(“/script/data.txt”)%><BR>
<%= Server.MapPath(“\script”)%><BR>
The preceding scripts produce the following output:
c:\inetpub\wwwroot\script\data.txt<BR>
c:\inetpub\wwwroot\script<BR>

<%= Server.MapPath(“/”)%><BR>
<%= Server.MapPath(“”)%><BR>
The preceding scripts produce the following output:
c:\inetpub\wwwroot<BR>
c:\inetpub\wwwroot<BR>

I found this great article by Fritz Onion. There is a zipped file from that site that you can download that will show you the different methods, but I thought that his explanation in the table below was sufficient:

Table 1 Path Functions in ASP.NET and Their Results

Expression

Evaluation

this.TemplateSourceDirectory

/informit/subdir

Request.MapPath("log.txt")

c:\mywebdirs\informit\subdir\log.txt

this.MapPathSecure("log.txt")

c:\mywebdirs\informit\subdir\log.txt

Request.Path

/informit/subdir/pathsample.aspx/extra

Request.FilePath

/informit/subdir/pathsample.aspx

Request.CurrentExecutionFilePath

/informit/subdir/pathsample.aspx

Request.PathInfo

/extra

Request.PhysicalPath

c:\mywebdirs\informit\subdir\pathsample.aspx

Request.PhysicalApplicationPath

c:\mywebdirs\informit\

Request.ApplicationPath

/informit

Request.Url

http://localhost/informit/subdir/client.aspx/extra

Request.RawUrl

/informit/subdir/pathsample.aspx/extra

Response.ApplyAppPathModifier("foo.aspx")

/informit/subdir/foo.aspx

this.ResolveUrl("~/client.aspx")

/informit/pathsample.aspx

Advertisements

Written by matt

September 19, 2007 at 7:03 am

Posted in Asp.net, VB.net

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: