.net core跨平台的文件路徑


windows下路徑為:"xxxx\\yyyy"

linux路徑下為:"xxxx/yyyy"

用Path.Combine("xxxx","yyyy");進行合並

-------------------------------------------------

通過nuget引入包 System.Runtime.InteropServices

RuntimeInformation.IsOSPlatform(OSPlatform.Linux)//判斷是否為Liunx

RuntimeInformation.IsOSPlatform(OSPlatform.Windows)/判斷是否為Win

RuntimeInformation.IsOSPlatform(OSPlatform.OSX)/判斷是否為OS
----------------------------------------

docker環境下用Environment.CurrentDirectory,不要用AppDomain.CurrentDomain.BaseDirectory

----------------------------------------------------

Web根目錄和內容根目錄

    public class HomeController : Controller
    {
        private readonly IHostingEnvironment _hostingEnvironment;

        public HomeController(IHostingEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }

        public ActionResult Index()
        {
            string webRootPath = _hostingEnvironment.WebRootPath;
            string contentRootPath = _hostingEnvironment.ContentRootPath;

            return Content(null);
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM