Home All Groups Group Topic Archive Search About

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

Author
20 Mar 2006 2:35 PM
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

Author
20 Mar 2006 5:53 PM
sloan
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.



Show quote
"Mowie_X" <merodg***@gmail.com> wrote in message
news:1142865330.123713.322400@i40g2000cwc.googlegroups.com...
> 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
>
Author
21 Mar 2006 2:14 AM
Mowie_X
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).

AddThis Social Bookmark Button