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.
Rohit started Embedded Programing in his college days and now he is a Software Developer by Profession. Mainly he interested in cutting edge technology offer by Microsoft (i.e Azure,MVC). He Loves coding and his passion is always been towards Microsoft Technologies. Apart from coding his other hobbies include reading books and hang out with friends is his most favorite past time hobby.
1. 20 Apr 2014: Best Mobile Article of March 2014 - First Prize