Discovery of winnings or web forms

Any reliable elegant way to determine if the current application is a win-win or web form (or other) application?

We have a generic config class that needs to open either app.config or web.config.

I am currently catching an ArgumentException when trying to OpenExeConfiguration, but it is not very graceful and may mask other issues.

0


a source to share


3 answers


I usually check to see if the HttpContext is available (if it's a web application, since it HttpContenxt.Current

is null in the web service ) To do this, you must add System.Web

to your links.



if(HttpContext.Current!=null)
//It a web application
else
//it a win application

      

+3


a source


Try using dependency injection so the config class doesn't have to execute the switch statement.



+1


a source


You can try to get the process running through Process.GetCurrentProcess()

, maybe you could check Process.MainWindowHandle

not IntPtr.Zero

, or check the process name, or potentially scan the loaded modules of the process. This would have the advantage (I would guess) of not requiring the loading of nodes that are not required by your current execution context (for example, not loading System.Windows.Forms.dll when this application is a web application).

However, this seems inelegant.

0


a source







All Articles