W~C停用

导航

.net Core 获取当前程序路径

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

posted on 2021-04-26 22:12  W~C停用  阅读(928)  评论(0编辑  收藏  举报