Azure Storage - 32: Azure File Service + .NET - Directories


In the previous post I showed how to work with Azure File Service shares from code. In this post I am going how to work with Azure File Service directories.


Refer to previous posts to get started with Azure File Service.


Azure File Service supports files and folders, there is a special folder - root folder. To get a reference to the root folder run the following code:

var share = fileClient.GetShareReference($"share1");
var root = share.GetRootDirectoryReference();

This folder always exists for any file share and you can't delete it. Consider the following example:

var root = share.GetRootDirectoryReference();

This code throws exception:

The remote server returned an error: (405) Method Not Allowed.

Root folder has methods that you can use to access ordinary files and folders. Take a look on the following code:

var share = fileClient.GetShareReference($"share2");
var root = share.GetRootDirectoryReference();
var dir = root.GetDirectoryReference("dir");

This code creates a new directory called "dir" in the root directory. The following code creates 50 directories inside "dir":

for (var i = 0; i < 50; i++)
    var subDir = dir.GetDirectoryReference($"sub-dir-{i}");

To get list of directories use the following code:

var dirs = dir.ListFilesAndDirectories();

You can delete all sub-directories in a similar way:

var subdirs = dir.ListFilesAndDirectories().Where(item => item is CloudFileDirectory).Cast<CloudFileDirectory>();
foreach (var cfd in subdirs)


In this post I have showed how to work with Azure File Service directories and provided basic code samples. In the next post I will show how to work with files.