AppDomain.BaseDirectory 属性

定义

获取基目录,它由程序集冲突解决程序用来探测程序集。

C#
public string BaseDirectory { get; }
C#
public string? BaseDirectory { get; }

属性值

String

基目录,由程序集冲突解决程序用来探测程序集。

实现

例外

在卸载的应用程序域上尝试该操作。

示例

下面的代码示例创建一个新的应用程序域,指定在搜索要加载到域中的程序集时要使用的基目录。 然后,该示例使用 BaseDirectory 属性获取基本目录路径,以便显示到控制台。

C#
using System;
using System.IO;
using System.Reflection;
using System.Security.Policy;

class ADSetup
{
    public static void Main()
    {
        // Create application domain setup information
        var domaininfo = new AppDomainSetup();
        domaininfo.ConfigurationFile = System.Environment.CurrentDirectory +
                                       Path.DirectorySeparatorChar +
                                       "ADSetup.exe.config";
        domaininfo.ApplicationBase = System.Environment.CurrentDirectory;

        //Create evidence for the new appdomain from evidence of the current application domain
        Evidence adEvidence = AppDomain.CurrentDomain.Evidence;

        // Create appdomain
        AppDomain domain = AppDomain.CreateDomain("Domain2", adEvidence, domaininfo);

        // Display application domain information.
        Console.WriteLine("Host domain: " + AppDomain.CurrentDomain.FriendlyName);
        Console.WriteLine("Child domain: " + domain.FriendlyName);
        Console.WriteLine();
        Console.WriteLine("Configuration file: " + domain.SetupInformation.ConfigurationFile);
        Console.WriteLine("Application Base Directory: " + domain.BaseDirectory);

        AppDomain.Unload(domain);
    }
}
// The example displays output like the following:
//    Host domain: adsetup.exe
//    Child domain: Domain2
//
//    Configuration file: C:\Test\ADSetup.exe.config
//    Application Base Directory: C:\Test

注解

此属性对应于 AppDomainSetup.ApplicationBase 该属性。 还可以使用 GetData 字符串“APPBASE”的方法检索它。

适用于

产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1

另请参阅