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.