Get Azure Storage Account Regions using c#

Roy_Jaze 40 Reputation points
2024-07-20T04:13:51.0866667+00:00

I need help with getting a list of all storage account regions using c# code. I tried creating a storage account in an unavailable location and catching the exception to get the regions, but I'm wondering if there's a better way to do this? Can someone share c# code that retrieves all the regions? Thanks in advance.

Attached are some screenshots for reference:

Screenshot 2024-07-20 093345

User's image

User's image

Azure Storage Explorer
Azure Storage Explorer
An Azure tool that is used to manage cloud storage resources on Windows, macOS, and Linux.
252 questions
Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,082 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,774 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Nehruji R 6,966 Reputation points Microsoft Vendor
    2024-07-22T10:23:49.37+00:00

    Hello Roy_Jaze,

    Greetings! Welcome to Microsoft Q&A Platform.

    You can retrieve a list of all Azure regions using the Azure Management Libraries for .NET. Here’s a sample C# code snippet that demonstrates how to do this:

    using System;
    using System.Threading.Tasks;
    using Azure.Identity;
    using Azure.ResourceManager;
    using Azure.ResourceManager.Resources;
    
    class Program
    {
        static async Task Main(string[] args)
        {
            var credential = new DefaultAzureCredential();
            var armClient = new ArmClient(credential);
    
            var subscription = await armClient.GetDefaultSubscriptionAsync();
            var locations = subscription.GetLocationsAsync();
    
            await foreach (var location in locations)
            {
                Console.WriteLine(location.DisplayName);
            }
        }
    }
    
    

    This code uses the Azure.Identity and Azure.ResourceManager libraries to authenticate and list all available regions for your subscription. Make sure you have the necessary NuGet packages installed:

    dotnet add package Azure.Identity
    dotnet add package Azure.ResourceManager
    
    

    This approach is more efficient and reliable than creating a storage account in an unavailable location and catching exceptions.

    Note: The above is an example code - Please modify per your requirements.

    Similar thread for reference - https://stackoverflow.com/questions/44143981/is-there-an-api-to-list-all-azure-regions, https://github.com/Azure-Samples/storage-dotnet-resource-provider-getting-started, https://video2.skills-academy.com/en-us/samples/azure-samples/storage-dotnet-resource-provider-getting-started/storage-dotnet-resource-provider-getting-started/

    This link provides detailed information of regions availability and also product by region

    https://azure.microsoft.com/en-in/global-infrastructure/services/?products=storage&regions=all.

    Hope this information helps! Please let us know if you have any further queries. I’m happy to assist you further.   


    Please "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.