确定是否没有的System.Web在ASP.NET或的WinForms /控制台上运行 [英] Determine whether running in ASP.NET or WinForms/console without System.Web

查看:143
本文介绍了确定是否没有的System.Web在ASP.NET或的WinForms /控制台上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个类库,我需要确定在运行时无论我在ASP.NET应用程序或一个WinForms /控制台应用程序运行。已经有<一个href="http://stackoverflow.com/questions/178306/how-to-deterimine-whether-net-dll-is-used-in-a-gui-app-or-web-service">several <一href="http://stackoverflow.com/questions/820595/net-determine-at-run-time-whether-my-app-is-an-exe-or-a-web-app">other <一href="http://stackoverflow.com/questions/2653969/how-do-i-determine-if-the-executing-assembly-is-a-web-app-or-winform-console">questions <一href="http://stackoverflow.com/questions/3179716/how-determine-if-application-is-web-application">asked关于这个问题,但所有这些解决方案都需要添加一个参考System.Web程序。如果可能的话,运行我的控制台和WinForms应用程序的时候,我不希望加载System.Web程序集到内存只为一行code出千。

From a class library, I need to determine at run-time whether I'm running in an ASP.NET app or a WinForms/console app. There have been several other questions asked on the subject, but all of those solutions require adding a reference to System.Web. If at all possible, when running my console and WinForms apps, I don't want to load the System.Web assembly into memory just for one line of code out of thousands.

推荐答案

使用 System.Diagnostics.Process.GetCurrentProcess()。ProcessName

如果你正在运行ASP.NET然后组装会正是如此命名为:

If you're running ASP.NET then the assembly will be named thusly:

  • 如果您运行的是IIS 6.0或者IIS 7.0,名称为w3wp.exe的。

  • If you are running IIS 6.0 or IIS 7.0, the name is w3wp.exe.

如果您正在运行IIS的早期版本,名称是ASPNET_WP.EXE。

If you are running an earlier version of IIS, the name is aspnet_wp.exe.

另外一个想法:如何测试过程/应用程序域的System.Web.dll中的presence与AppDomain.CurrentDomain.GetAssemblies()API

Another Idea: How about testing the process/app domain for the presence of the System.Web.dll with the AppDomain.CurrentDomain.GetAssemblies() API?

这篇关于确定是否没有的System.Web在ASP.NET或的WinForms /控制台上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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