Search code examples
asp.net-corepathfilepathgetdirectoriespath-combine

Error when trying to read a file via relative path in .net core 3.X in production


When I'm running my project in localhost I'm able to locate the file and process it further. This is achieved with this line of code.

path = Path.Combine(Directory.GetCurrentDirectory(), "EmailTemplates\\SuccessOrderWindows10.html");

I'm able to get the full relative path C:\etc\etc\etc.. But when i push this code to production, when it reaches to this stage, it throws an error

Error One or more occurred. (Could not find a part of the path 'h:\root\home\username\www\sitename\EmailTemplates\SuccessOrderWindows10.html'.)

What I'm doing wrong? Do i have to select the files and set them to content so that it will be included in the build?


Solution

  • Directory.GetCurrentDirectory will get the current working directory of the application.When you push the code to production,you change the the current working directory,so the relative path of your file will change,so you need to put the file to the new path h:\root\home\username\www\sitename\EmailTemplates\SuccessOrderWindows10.html when production.Or you can use absolute path.