【问题标题】:How to store file paths in a cross platform way in a .json configuration file in ASP.NET Core?如何在 ASP.NET Core 的 .json 配置文件中以跨平台方式存储文件路径?
【发布时间】:2020-09-07 06:51:56
【问题描述】:

如何在ASP.NET Core的.json配置文件中跨平台存储文件路径?

例如我在配置中有条目:

MyPath: "C:\\Foo\\Bar"

并且该路径适用于 Windows,但不适用于 Linux。我知道Path.DirectorySeparatorChar,但我如何在.json 中使用它?还是我应该使用不同的东西?

【问题讨论】:

    标签: asp.net-core directory configuration path cross-platform


    【解决方案1】:

    试试看:

                        // IWebHostEnvironment _webHost
                        // IConfiguration Configuration
                        var listOfPath = Configuration["my-path"].ToString()
                            .Split("\\", StringSplitOptions.RemoveEmptyEntries);
    
                        var uploadPath = Path.Combine(_webHost.WebRootPath );
                        foreach (var folders in listOfPath)
                        {
                            uploadPath = Path.Combine(uploadPath, folders);
                        }
    

    【讨论】:

    • 为什么要使用toString() 方法?无论如何,配置返回为string
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 2014-06-09
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多