[英]What's the difference between Application and AppDomain.CurrentDomain
I am little confused about usage of Application
class and AppDomain
class. 我对
Application
类和AppDomain
类的使用感到困惑。
For example Application.StartupPath
is equal to AppDomain.CurrentDomain.BaseDirectory
例如,
Application.StartupPath
等于AppDomain.CurrentDomain.BaseDirectory
I usually used Application
class and recently discovered AppDomain
- Can someone explain to me AppDomain
class and its usage? 我通常使用
Application
类并且最近发现了AppDomain
- 有人可以向我解释AppDomain
类及其用法吗?
They have nothing in common, really. 他们没有任何共同点,真的。
Application
is a class specific to Windows Forms, a .NET GUI technology. Application
是特定于Windows Forms(.NET GUI技术)的类。 The Application.StartupPath
is handled by the Kernel32 function GetModuleFileName
. Application.StartupPath
由Kernel32函数GetModuleFileName
处理。 Through not passing a pointer to a module, the main module's path is returned - that is the exe
file, basically. 通过不传递指向模块的指针,返回主模块的路径 - 基本上就是
exe
文件。
AppDomain
is a core .NET concept for domain isolation. AppDomain
是域隔离的核心.NET概念。 Basically, it allows you to isolate (imperfectly of course) multiple applications in a single native process. 基本上,它允许您在单个本机进程中隔离(当然不完美)多个应用程序。 Most applications only have a single
AppDomain
, but you can create as many as you like. 大多数应用程序只有一个
AppDomain
,但您可以根据需要创建任意数量的AppDomain
。 The base path of an application domain is handled by Fusion, a .NET assembly loading technology. 应用程序域的基本路径由Fusion(一种.NET程序集加载技术)处理。 A very typical example would be ASP.NET applications hosted in IIS - each application has its own
AppDomain
, but they're all hosted in a single native process ("application pool"). 一个非常典型的例子是IIS中托管的ASP.NET应用程序 - 每个应用程序都有自己的
AppDomain
,但它们都托管在一个本机进程(“应用程序池”)中。 Each logical application can be restarted without touching the others, and they don't have (simple) access to each other, but a process-tearing exception (eg StackOverflowException
) will still kill the whole pool. 每个逻辑应用程序都可以在不触及其他逻辑应用程序的情况下重新启动,并且它们之间没有(简单)访问权限,但是流程撕裂异常(例如
StackOverflowException
)仍将终止整个池。
Another interesting class that's somewhat related is Environment
. 另一个有趣的课程是
Environment
。 You can use Environment.CommandLine
to get the process command line (which includes the path to the executable, including the name of the executable). 您可以使用
Environment.CommandLine
来获取进程命令行(包括可执行文件的路径,包括可执行文件的名称)。 This is basically a communication interface between the CLR and the underlying system - in this case, it takes care of saving the arguments for the application (which are passed by the OS to the Main
function) and making them available at any time in the future. 这基本上是CLR和底层系统之间的通信接口 - 在这种情况下,它负责保存应用程序的参数(由OS传递给
Main
函数)并在将来的任何时候使它们可用。
Environment.CommandLine
is somewhat clunky to parse (it's the raw command-line, basically - I assume it will have different conventions on Windows than on Linux, for example), but it's the only way you can always get to the executable. Environment.CommandLine
有点笨拙解析(它是原始命令行,基本上 - 我认为它在Windows上会有不同于Linux上的约定),但它是你总能找到可执行文件的唯一方法。 Again, Application.StartupPath
is Winforms specific, and you can have more than one AppDomain
- and possibly, the AppDomain
might not even have a reasonable BaseDirectory
. 同样,
Application.StartupPath
是特定于Winforms的,并且您可以拥有多个AppDomain
- 可能, AppDomain
可能甚至没有合理的BaseDirectory
。
The .NET Reflection APIs also give you a few ways. .NET Reflection API还为您提供了几种方法。 For example,
Assembly.GetEntryAssembly()
will give you the executable assembly - however, this only works for the main AppDomain
- other domains will have their own entry assemblies (in fact, they'll usually just return null
:)). 例如,
Assembly.GetEntryAssembly()
将为您提供可执行程序集 - 但是,这仅适用于主AppDomain
- 其他域将具有自己的条目程序集(实际上,它们通常只返回null
:))。 You can get the path to an assembly through the Assembly.CodeBase
property, but do note that this might not always be what you expect. 您可以通过
Assembly.CodeBase
属性获取程序集的路径,但请注意,这可能并不总是您所期望的。 You can also use Assembly.Location
, or get the FullyQualifiedName
of any of the assembly's modules (again, most assemblies only have a single module; and again, ASP.NET is one of the prime examples of when this isn't the case). 您还可以使用
Assembly.Location
,或获取任何程序集模块的FullyQualifiedName
(同样,大多数程序集只有一个模块;而且,ASP.NET不是这种情况的主要示例之一) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.