1、获取引用程序目录,即运行时代码目录
1 AppContext.BaseDirectory;
结果为:....\ApplicationName\bin\Debug\net5.0\
2、获取文件目录
1 using Microsoft.Extensions.Hosting; 2 3 private readonly IHostEnvironment _hostEnvironment; 4 5 public TestController(IHostEnvironment hostEnvironment) 6 { 7 _hostEnvironment = hostEnvironment; 8 } 9 10 public IActionResult Test() 11 { 13 return Content(_hostEnvironment.ContentRootPath); 14 }
结果为:
结果为:....\ApplicationName