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?).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?System.Web
and System.Windows.Forms
in a helper library should be fine, according to this thread.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.
Check whether HttpContext.Current
is null
. If it is, it's not a webapp.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With