Note: I posted this quite awhile back at FooTheory but it didn’t transfer over to this blog via cross posting for some reason. Internet-wackiness I call it.
This is an oldie but a goodie. It was brought up that not one developer at my current client knew about this object in the framework.
So, how do you find where the app is running at?
Easy: Use the AppDomain object. This works for both Web and Non-Web projects.
Code:
1 AppDomain.CurrentDomain.BaseDirectory
The AppDomain object will return the application’s domain information, which includes the info about where it is executing. We get the current domain of the executing thread and then get its BaseDirectory to see where it’s executing.
This:
Returns this:
Enjoy.
ponki.d.monkey says
hi,
i’m currently using AppDomain.CurrentDomain.BaseDirectory in one of my personal projects. it’s working well in the local environment but i’m just not quite sure if it will work in a shared hosting environment like godaddy once i have deployed the application. any idea on this?
Donn Felker says
To tell you the truth, I’m not exactly sure if it will work or not.
I’m thinking it might not since I believe it requieres some sort of locatin
permission by code access security and GoDaddy is notorious for locking
everything down tight. You might want to upload a test page and have it
write out the base directory to the response. In Page_Load you’d do:
Response.Write(AppDomain.CurrentDomain.BaseDirectory) to see if it would
work.
Let me know what you find out. I dont have any items hosted on GoDaddy, but
I’m sure others would like to know if they stumble upon this post. ๐
ponki.d.monkey says
sorry for a really, really late reply. i’ve been so busy the past few weeks. anyway, i already tried using AppDomain.CurrentDomain.BaseDirectory on one of my websites hosted on godaddy and surprisingly it worked! no security issues whatsoever. here’s the code that i used to see if it would work:
protected void Page_Load(object sender, EventArgs e)
{
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
string image = string.Format(“{0}\images\blank.gif”, baseDir);
if (File.Exists(image))
Response.Write(“YES, it exists!!”);
}
i hope this helps. ~cheers!