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



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

例如,我在配置中有一个条目:

MyPath: "C:\Foo\Bar"

该路径适用于Windows,但不适用于Linux。我知道Path.DirectorySeparatorChar,但如何在.json中使用它?或者我应该用一些不同的东西?

试试看:

// 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);
}

最新更新