Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Hosted-services / Azure

Getting Your Physical Application Path in Window Azure

4.00/5 (1 vote)
19 Dec 2010CPOL1 min read 29.5K  
cloud
Some applications access files from a relative path in the hosted application directory. For example, you might want to launch an executable or open a file (such as in my example later in this article when I load an X.509 certificate). To achieve this, you must access the physical application path or file system path. This path is made available through the environment variable RoleRoot.

RoleRoot returns different results when you run in the development fabric versus when it's deployed to Azure (the cloud). Running in the development fabric returns a path such as this one for a worker role project:

D:\Solution\CloudProject\bin\Debug\CloudProject.csx\roles\RoleProject

When running in the cloud you get
E: Drive PAth


In either case, you want to append “approot” to the end of the path, and from there, your deployed files follow your application structure. If you have subdirectories with files or a \bin directory for typical ASP.NET deployments, they are all relative to approot. Note that there isn't a “\” after the drive letter in the cloud, so you need code like the following example to produce a proper approot path:

string appRoot = Environment.GetEnvironmentVariable("RoleRoot");

appRoot = Path.Combine(appRoot + @"\", @"approot\");


To produce a file path, use this code:

string privateKeyCert = "busta-rp.com.pfx";

string appRoot = Environment.GetEnvironmentVariable("RoleRoot");

string pathToPrivateKey = Path.Combine(appRoot + @"\", string.Format(@"approot\{0}", privateKeyCert));


This code will work for either development or cloud deployments, so you don't have to adjust your code when you deploy.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)