Pro Tip: Getting the physical directory / path for your Azure Worker Role

14 December 2015

Here's a quick one - adding files to your Azure Worker Role is pretty simple, through Visual Studio - everything under Roles\[YourWorkerRole] is automatically packaged up and deployed to Azure (as described here). Adding files is easy - but what about accessing them?

It turns out that this isn't too difficult too. Azure provides you with a "RoleRoot" environment variable that you can use both locally and in the cloud (so your code for accessing both should be identical). If you've read the link about adding files above, you'll note that all content is deployed to an AppRoot directory, so if you've got the following in your solution:

MyWork.AzureWorkerProject
-- Roles
---- MyWork.AzureWorkerProject.WorkerRole
------ images
-------- test.png

The following code should return you the correct file path for test.png, regardless of whether you're running in dev locally or in the cloud.

string appRoot = Environment.GetEnvironmentVariable("RoleRoot");   
string fullPath = Path.Combine(appRoot + @"\", @"AppRoot\images\test.png");

Simple, right? Have fun, Merry Christmas! (Where has the year gone?!)

Add a Comment

No Comments