Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if app is WinForms or WebForms

Is there a way to determine if running app is WinForms or Web at runtime?

[Edit]

Is there actually a problem if I reference both System.Web and System.Windows.Forms in my class library?

[Summary] (so far)

What I've learned so far:

  • HttpContext.Current is null if checked in an async thread, so it cannot reliably be used in a helper method. HttpRuntime.Cache doesn't really help since I am not looking for the cached context at all (or am I missing something here?).
  • on the other hand, System.Reflection.Assembly.GetEntryAssembly() seems to return null in web apps, and non null in WinForms. Should that be taken for granted? There should be more "hacks" like this, so which one to use?
  • referencing both System.Web and System.Windows.Forms in a helper library should be fine, according to this thread.
like image 927
dummy Avatar asked Jun 02 '10 10:06

dummy


2 Answers

In your edit you specify that your helper class is in a seperate assembly. If you want to avoid references and you have control over your app.config files you could put an

<add key="typeOfSystem" value="Forms|Web"/>

in your projects and access it with

ConfigurationManager.AppSettings["typeOfSystem"]

using ConfigurationManager along with its property AppSettings. Don't forget to add a reference to System.Configuration in your project. Note that you get the AppSettings of the hosting application. By doing this you need to add a key in your parent application's app.config, or output some error or warning if it's not specified perhaps.

like image 199
Patrick Avatar answered Sep 24 '22 23:09

Patrick


Check whether HttpContext.Current is null. If it is, it's not a webapp.

like image 26
Anton Gogolev Avatar answered Sep 22 '22 23:09

Anton Gogolev