如何在 ASP.NET Core 1.0 中获取 bin 文件夹

How to get bin folder in ASP.NET Core 1.0

使用 asp.net 核心 1.0 添加了许多功能。但是没有办法获取 Bin 文件夹路径。

任何人都可以知道我们如何获得 asp.net 核心 1.0 应用程序的 bin 文件夹路径。

您无法真正获取 /bin/ 文件夹,因为它与您的项目无关,并且 ASP.NET 环境不知道 /bin/ 文件夹是什么。

而且也没有 /bin/ 文件夹。您可能想阅读这篇文章:http://docs.asp.net/en/latest/conceptual-overview/understanding-aspnet5-apps.html

但是您可以获得所谓的 ApplicationBasePath,这是您的应用程序运行的目录:

public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
     string baseDir = appEnv.ApplicationBasePath;
     // Other startup code
}

好吧,bin 文件夹确实存在,但它已移至解决方案文件旁边的 artifacts 文件夹。由于 ASP.NET Core RC 1 在内存中编译所有内容,您会发现空的 bin 文件夹。但是,如果您将 "Produce output on build" 选项设置为 true(右键单击项目文件 -> 属性和构建选项卡),那么您将在 bin 文件夹中找到生成的文件。

我认为没有任何直接的 属性 可用于获取此路径,但您可以使用@Nikolay Kostov 指出的相同解决方案来获取应用程序路径。然后使用 System.IO 类 跳转到 bin 文件夹。

代码已更新为 ASP.NET 核心,如此处所述。

http://www.talkingdotnet.com/get-application-wwwroot-path-aspnet-core-rc2/

public Startup(IHostingEnvironment env, IApplicationEnvironment appenv)
{
     string sAppPath = env.ContentRootPath;
     string sRootPath = Path.GetFullPath(Path.Combine(sAppPath, @"..\..\"));
     string sBinFolderPath = @"artifacts\bin\" + appenv.ApplicationName;
     string sBinPath = Path.Combine(sRootPath, sBinFolderPath);
}

这用于检索程序集的目录,我们可以从中确定 bin 位置。

var location = System.Reflection.Assembly.GetEntryAssembly().Location;
var directory = System.IO.Path.GetDirectoryName(location);
System.Console.WriteLine(directory);

输出

C:\MyApplication\bin\Debug\netcoreapp1.0

备选方式(对应AppDomain.BaseDirectory):

AppContext.BaseDirectory

AppDomain.CurrentDomain.BaseDirectory;