从应用程序域中检索安装信息
注意
本文特定于 .NET Framework。 它不适用于 .NET 的较新版本实现,包括 .NET 6 及更高版本。
应用程序域的每个实例由属性和 AppDomainSetup 信息组成。 可使用 System.AppDomain 类从应用程序域中检索安装信息。 此类提供多个成员,可用于检索应用程序域的相关配置信息。
还可以查询应用程序域的 AppDomainSetup 对象,获取创建域时传递到该域的安装信息。
下面的示例创建新的应用程序域,然后将几个成员值打印到控制台。
using namespace System;
using namespace System::Reflection;
ref class AppDomain3
{
public:
static void Main()
{
// Create the new application domain.
AppDomain^ domain = AppDomain::CreateDomain("MyDomain", nullptr);
// Output to the console.
Console::WriteLine("Host domain: " + AppDomain::CurrentDomain->FriendlyName);
Console::WriteLine("New domain: " + domain->FriendlyName);
Console::WriteLine("Application base is: " + domain->BaseDirectory);
Console::WriteLine("Relative search path is: " + domain->RelativeSearchPath);
Console::WriteLine("Shadow copy files is set to: " + domain->ShadowCopyFiles);
AppDomain::Unload(domain);
}
};
int main()
{
AppDomain3::Main();
}
using System;
using System.Reflection;
class AppDomain3
{
public static void Main()
{
// Create the new application domain.
AppDomain domain = AppDomain.CreateDomain("MyDomain", null);
// Output to the console.
Console.WriteLine("Host domain: " + AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("New domain: " + domain.FriendlyName);
Console.WriteLine("Application base is: " + domain.BaseDirectory);
Console.WriteLine("Relative search path is: " + domain.RelativeSearchPath);
Console.WriteLine("Shadow copy files is set to: " + domain.ShadowCopyFiles);
AppDomain.Unload(domain);
}
}
Imports System.Reflection
Class AppDomain3
Public Shared Sub Main()
' Create the new application domain.
Dim domain As AppDomain = AppDomain.CreateDomain("MyDomain", Nothing)
' Output to the console.
Console.WriteLine("Host domain: " + AppDomain.CurrentDomain.FriendlyName)
Console.WriteLine("New domain: " + domain.FriendlyName)
Console.WriteLine("Application base is: " + domain.BaseDirectory)
Console.WriteLine("Relative search path is: " + domain.RelativeSearchPath)
Console.WriteLine("Shadow copy files is set to: " + domain.ShadowCopyFiles)
AppDomain.Unload(domain)
End Sub
End Class
下面的示例设置并检索某一应用程序域的安装信息。 使用 AppDomain.SetupInformation.ApplicationBase
可获取配置信息。
using namespace System;
using namespace System::Reflection;
ref class AppDomain5
{
public:
static void Main()
{
// Application domain setup information.
AppDomainSetup^ domaininfo = gcnew AppDomainSetup();
domaininfo->ApplicationBase = "f:\\work\\development\\latest";
domaininfo->ConfigurationFile = "f:\\work\\development\\latest\\appdomain5.exe.config";
// Creates the application domain.
AppDomain^ domain = AppDomain::CreateDomain("MyDomain", nullptr, domaininfo);
// Write the application domain information to the console.
Console::WriteLine("Host domain: " + AppDomain::CurrentDomain->FriendlyName);
Console::WriteLine("Child domain: " + domain->FriendlyName);
Console::WriteLine();
Console::WriteLine("Application base is: " + domain->SetupInformation->ApplicationBase);
Console::WriteLine("Configuration file is: " + domain->SetupInformation->ConfigurationFile);
// Unloads the application domain.
AppDomain::Unload(domain);
}
};
int main()
{
AppDomain5::Main();
}
using System;
using System.Reflection;
class AppDomain5
{
public static void Main()
{
// Application domain setup information.
AppDomainSetup domaininfo = new AppDomainSetup();
domaininfo.ApplicationBase = "f:\\work\\development\\latest";
domaininfo.ConfigurationFile = "f:\\work\\development\\latest\\appdomain5.exe.config";
// Creates the application domain.
AppDomain domain = AppDomain.CreateDomain("MyDomain", null, domaininfo);
// Write the application domain information to the console.
Console.WriteLine("Host domain: " + AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("Child domain: " + domain.FriendlyName);
Console.WriteLine();
Console.WriteLine("Application base is: " + domain.SetupInformation.ApplicationBase);
Console.WriteLine("Configuration file is: " + domain.SetupInformation.ConfigurationFile);
// Unloads the application domain.
AppDomain.Unload(domain);
}
}
Imports System.Reflection
Class AppDomain5
Public Shared Sub Main()
' Application domain setup information.
Dim domaininfo As New AppDomainSetup()
domaininfo.ApplicationBase = "f:\work\development\latest"
domaininfo.ConfigurationFile = "f:\work\development\latest\appdomain5.exe.config"
' Creates the application domain.
Dim domain As AppDomain = AppDomain.CreateDomain("MyDomain", Nothing, domaininfo)
' Write the application domain information to the console.
Console.WriteLine("Host domain: " + AppDomain.CurrentDomain.FriendlyName)
Console.WriteLine("Child domain: " + domain.FriendlyName)
Console.WriteLine()
Console.WriteLine("Application base is: " + domain.SetupInformation.ApplicationBase)
Console.WriteLine("Configuration file is: " + domain.SetupInformation.ConfigurationFile)
' Unloads the application domain.
AppDomain.Unload(domain)
End Sub
End Class