Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get local file system path in azure websites

Tags:

azure

I have a file hosted on the disk along with my website that I want to read .Not sure how do I access the file when I use System.Environment.CurrentDirectory it point to a D drive location .Can someone please tell me how can I get to my file stored at the root of where my site is hosted.

Thanks

like image 844
sp9 Avatar asked Jun 20 '14 18:06

sp9


People also ask

How do I access the Azure App Service file system?

Console Access to my App Service I can go to the Azure Portal and select my App Service and click on Console under Development Tools to have a command prompt to quickly work with my Azure App Service. As you can tell from the screen-shot, I start in D:\home\site\wwwroot .


2 Answers

There is an environment variable called HOME in your website's environment that will get you part way there.

You can access it using razor syntax or in code (C#). For example, suppose you have a file called data.txt that is at the root of your site with the default document and the rest of your files. You could get it's full path like this.

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

You can find this out on your own using the Site Control Management/"Kudu". For example, if your website is contoso.azurewebsites.net, then simply navigate to contoso.scm.azurewebsites.net. In here you can learn all about the file system and environment variables available to your website.

like image 79
Rick Rainey Avatar answered Oct 09 '22 22:10

Rick Rainey


For testability, I use below code.

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

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

like image 26
Youngjae Avatar answered Oct 09 '22 22:10

Youngjae