How to get the path to the local file system on azure sites

I have a file located on disk along with my website that I want to read. Not sure how I can access the file when I use System.Environment.CurrentDirectory, it points to the location for drive D. Can someone tell me how I can get to my file stored in the root directory of my site.

thanks

+7
azure
source share
3 answers

There is an environment variable in your websiteโ€™s environment called HOME to help you with this.

You can access it using razor syntax or code (C #). For example, suppose you have a file called data.txt, which is located at the root of your site with the default document and the rest of your files. You can get the full path as follows.

@{ var dataFileName = Environment.GetEnvironmentVariable("HOME").ToString() + "\\site\\wwwroot\\data.txt"; } 

You can find it yourself using Site Control / "Kudu". For example, if your site is contoso.azurewebsites.net, then just go to contoso. scm .azurewebsites.net. Here you can find out all about the file system and environment variables available on your website.

+11
source share

For verification, I use the code below.

 string path = ""; if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("HOME"))) path = Environment.GetEnvironmentVariable("HOME") + "\\site\\wwwroot\\bin"; else path = "."; path += "\\Resources\\myfile.json"; 

In the above example, I added the myfile.json file to the Resources folder in the project with the Content and Copy if newer .

+3
source share

This works for me in both localhost and azure:

 Path.Combine(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath, "file_at_root.txt"); 

System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath is the full local path to your site root.

+1
source share

All Articles