确定是否应用程序是的WinForms或WebForms的 [英] Determine if app is WinForms or WebForms

查看:191
本文介绍了确定是否应用程序是的WinForms或WebForms的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,以确定是否正在运行的应用程序是的WinForms或Web在运行时?

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

有没有实际上是一个问题,如果我同时引用的System.Web System.Windows.Forms的在我的类库?

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

[摘要] (到目前为止)

我到目前为止已经了解到:

What I've learned so far:


  • HttpContext.Current 检查是否在异步线程,因此它不能可靠地在一个辅助方法使用。 HttpRuntime.Cache 并没有真正帮助,因为我不是在寻找在所有缓存的上下文(或我思念的东西吗?)。

  • 在另一方面, System.Reflection.Assembly.GetEntryAssembly()似乎返回在Web应用程序和非空的WinForms的。应该说是理所当然的?应该有更多的黑客这个样子,所以要使用哪一个?

  • 引用既的System.Web System.Windows.Forms的在一个辅助库应该罚款,根据的此线程

  • 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.

推荐答案

在您的编辑您可以指定你的助手类是在一个单独的装配。如果你想避免引用,你必须控制你的app.config文件,你可以把一个

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"]

使用<一个HREF =http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx> ConfigurationManager中其属性的 AppSettings的。不要忘了参考您的项目中添加 System.Configuration 。请注意,你得到的宿主应用程序的AppSettings的。通过这样做,你的需求导入在父应用程序的app.config一个键,或输出一些错误,或者如果不是也许指定为鉴戒。

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.

这篇关于确定是否应用程序是的WinForms或WebForms的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆