1.先创建一个类:

 public static class MyServiceProvider
    {
        public static IServiceProvider ServiceProvider
        {
            get; set;
        }
    }

2.Startup.cs中Configure方法中添加    MyServiceProvider.ServiceProvider = app.ApplicationServices;     

 

 

.net Core 获取当前程序路径

 

 

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

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

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

 

效果如下:

.net Core 获取当前程序路径

 

 

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

 

4.如果需要获取UI下的wwwroot路径方法得改一下,为

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

 

相关文章:

  • 2022-12-23
  • 2021-09-23
  • 2021-07-27
  • 2022-03-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-17
  • 2022-12-23
  • 2021-09-26
  • 2021-12-17
  • 2021-05-25
  • 2021-11-21
  • 2022-12-23
相关资源
相似解决方案