How do I tell if I'm in Console or Web application?

  • Thread starter Thread starter Mowie_X
  • Start date Start date
M

Mowie_X

Hi,

How do I tell from within code whether or not I'm in a web or console
application? I need to know because it determines which config file I
am going to access.

There has to be a way via AppDomain or Environment, but I'm just not
finding it.

Thanks
 
Try this:


if ( null == System.Web.HttpContext.Current ) // Add a reference to
System.Web.dll
{ //Non Web Environment

}
else
{ //Web Environment

}


It may be fudgy on the first page you load. (Aka, when testing, you set a
start up page to something like "default.aspx".
I'm not 100% sure this is the case, I'm just suggesting to be aware of it.
 
Thanks a bunch. I found a similar kludgey solution. Using the
System.Diagnostics namespace.

I get the current process and try to get the MainWindowHandle.Title..
And then I see if it endswith "exe". Won't for a web app (will be
blank).
 
Back
Top