Asp.Net Core 获取应用程序相关目录

在ASP.NET Core中,可以通过以下三种方式获取应用程序所在目录:

1、使用`AppContext.BaseDirectory`属性:

string appDirectory = AppContext.BaseDirectory;

例如:D:\后端项目\testCore\test.WebApi\bin\Debug\net6.0\

2、使用`IHostEnvironment`服务: 在ASP.NET Core的启动文件(如Program.cs)中,可以通过依赖注入获取`IHostEnvironment`服务,并使用其`ContentRootPath`属性来获取应用程序所在目录:

string appDirectory = hostEnvironment.ContentRootPath;

例如:D:\后端项目\testCore\test.WebApi\wwwroot

3、使用`IHostingEnvironment`服务: 在较早版本的ASP.NET Core中,可以使用`IHostingEnvironment`服务获取应用程序所在目录。方法与上述相似:

string appDirectory = hostingEnvironment.ContentRootPath;

例如:D:\后端项目\testCore\test.WebApi\wwwroot

4、使用程序集信息获取typeof(Program)方式:

var executablePath = Assembly.GetEntryAssembly().Location;

string basePath2 =Path.GetDirectoryName(typeof(Program).Assembly.Location);

例如:D:\后端项目\testCore\test.WebApi\bin\Debug\net6.0\

5.依赖注入:

using System;

using Microsoft.AspNetCore.Hosting;

using Microsoft.Extensions.Hosting;

public class Program

{

public static void Main(string[] args)

{

CreateHostBuilder(args).Build().Run();

}

public static IHostBuilder CreateHostBuilder(string[] args) =>

Host.CreateDefaultBuilder(args)

.ConfigureWebHostDefaults(webBuilder =>

{

webBuilder.UseStartup<Startup>();

});

}

public class Startup

{

private readonly IHostEnvironment _hostEnvironment;

public Startup(IHostEnvironment hostEnvironment)

{

_hostEnvironment = hostEnvironment;

}

public void ConfigureServices(IServiceCollection services)

{

// 添加其他服务

string appDirectory = _hostEnvironment.ContentRootPath;

// 使用 appDirectory 进行其他操作

}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

{

// 配置中间件

app.Run(async (context) =>

{

string appDirectory = env.ContentRootPath;

await context.Response.WriteAsync($"Application Directory: {appDirectory}");

});

}

}

上面代码属NetCore早期版本,.NetCore3.1 后:

创建项目:

Startup中Configure自动注入IWebHostEnvironment:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

{

if (env.IsDevelopment())

{

app.UseDeveloperExceptionPage();

}

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>

{

endpoints.MapControllers();

});

}

Controller中直接注入即可:

private IWebHostEnvironment _evn;

public FileUploadController(IWebHostEnvironment evn)

{

this._evn = evn;

}

使用:string webroot = _evn.WebRootPath;//拿到 wwwroot 路径

6、创建类方式:

1.先创建一个类:

public static class MyServiceProvider

{

public static IServiceProvider ServiceProvider

{

get; set;

}

}
2.Startup.cs中Configure方法中添加 :

MyServiceProvider.ServiceProvider = app.ApplicationServices;

3.在需要用到程序路径的地方,引用:

using Microsoft.AspNetCore.Hosting;

using Microsoft.Extensions.DependencyInjection;

string path = MyServiceProvider.ServiceProvider.GetRequiredService<IHostingEnvironment>().ContentRootPath; //.NET Core 3.1,IHostingEnvironment 要改为IHostEnvironment

效果如下:

获取了路径:D:\\Project\\MyWebsite\\UI

4.如果需要获取UI下的wwwroot

路径方法得改一下,为

static string path = MyServiceProvider.ServiceProvider.GetRequiredService<IHostingEnvironment>().WebRootPath; //.NET Core 3.1,IHostingEnvironment 要改为IHostEnvironment

相关推荐
滴滴答答哒3 天前
.NET Core 基于 AOP + Polly 实现数据库死锁自动重试
数据库·.netcore·sqlsugar
.NET修仙日记6 天前
.NET EFCore批量插入性能优化实战:30秒 → 0.5秒
性能优化·c#·.net·.netcore·微软技术·efcore·踩坑实录
Kimhill张9 天前
.net core8 WPF 依赖注入(DI)
wpf·.netcore
wangl_9210 天前
C# / .NET 在工业环境中的优势
开发语言·c#·.net·.netcore·.net core·visual studio
豆豆13 天前
信创环境下CMS国产化适配实践:以.NET Core路线为例的技术验证
.netcore·cms·信创·国产化·建站系统·内容管理系统·网站管理系统
时光追逐者13 天前
C#/.NET/.NET Core技术前沿周刊 | 第 70 期(2026年5.01-5.10)
c#·.net·.netcore
van久18 天前
Day20:AutoMapper 对象映射
.netcore
van久18 天前
Day23 登录 + 颁发 Token(DDD 四层架构 + 企业标准)
.netcore
wangl_9219 天前
C#性能优化完全指南 - 从原理到实践
开发语言·性能优化·c#·.net·.netcore·visual studio
宝桥南山23 天前
GitHub Models - 尝试一下使用GitHub Models
microsoft·ai·微软·c#·github·.netcore