Create Folder Inside Azure Datalake Using C#

Folder creation inside azure datalake is a bit hard to implement using c#. Let me show you how to do so programmatically.

//Parameters

//Connectionstring  : Datalake connection string
//path : "ContainerName/folder1"
//newFolderName : "folder2"

public void CreateSubDirectories(string connectionString, string path,string newFolderName)
{
    var storageAccount = new DataLakeServiceClient(connectionString);           
    string[] strPath = path.Split("/");
    string container = string.Empty;
    string newFoldername = string.Empty;
    DataLakeFileSystemClient dirClient = storageAccount.GetFileSystemClient(strPath[0]);
    if (strPath.Length == 1) //Create folder inside container
    {
        DataLakeDirectoryClient directory = dirClient.GetDirectoryClient(newFolderName);
        directory.CreateIfNotExists();    
    }
    else
    {
        DataLakeDirectoryClient directory = dirClient.GetDirectoryClient(strPath[1]); //Create folder inside folder in container
       if (strPath.Length == 2)
       {
           directory.CreateSubDirectory(newFolderName);
           
       }
       else
       {
           //Create folder inside folder in container
           int i = 0;
           foreach (string str in strPath)
           {
               i++;
               if (i > 2 )
               {
                   container = container + str + ((i + 1 == strPath.Length) ? "" : "/");
               }
             
           }
           DataLakeDirectoryClient subdirectory = directory.GetSubDirectoryClient(container);
           subdirectory.CreateSubDirectory(newFolderName);
       }
   }
}