简体   繁体   English

Application和AppDomain.CurrentDomain之间有什么区别

[英]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.

相关问题 Application.ThreadException 和 AppDomain.CurrentDomain.UnhandledException 有什么区别? - What's the difference between Application.ThreadException and AppDomain.CurrentDomain.UnhandledException? AppDomain.CurrentDomain.BaseDirectory和Application.ExecutablePath在实践中有什么区别? - What's the difference between AppDomain.CurrentDomain.BaseDirectory and Application.ExecutablePath in practice? 将程序集加载到AppDomain中,但也加载到AppDomain.CurrentDomain中导致内存泄漏 - Loading an assembly into an AppDomain but also in the AppDomain.CurrentDomain resulting in memory leaks 将 AppDomain.CurrentDomain 基目录更改为另一个项目目录 - Changing the AppDomain.CurrentDomain Base Directory to another project directory 在AppDomain.CurrentDomain中获取所有用户创建的类 - Get all User-Created Classes in AppDomain.CurrentDomain CurrentDomain 和 UserDomainName 之间有什么区别? - What is the difference between CurrentDomain and UserDomainName? System.CurrentDomain.AppDomain.BaseDirectory和Directory.GetCurrentDirectory()之间的C#差异 - C#-Difference between System.CurrentDomain.AppDomain.BaseDirectory and Directory.GetCurrentDirectory() 编写 windows 窗体应用程序时 Application.StartupPath 和 AppDomain.CurrentDomain.BaseDirectory 之间的首选项 - Preference between Application.StartupPath and AppDomain.CurrentDomain.BaseDirectory when writing a windows forms application Application.ThreadException 和 AppDomain.CurrentDomain.UnhandledException 都不受尊重 - Neither Application.ThreadException nor AppDomain.CurrentDomain.UnhandledException are respected WPF应用程序忽略AppDomain.CurrentDomain.SetData值 - WPF Application ignoring AppDomain.CurrentDomain.SetData value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM