sdk/resourcemanager/Azure.ResourceManager/docs/Sample1_HelloWorld.md
Note: Before getting started with the samples, make sure to go trough the prerequisites.
Namespaces for this example:
using System;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Resources;
The following code shows how to get the default subscription:
ArmClient client = new ArmClient(new DefaultAzureCredential());
SubscriptionResource subscription = client.GetDefaultSubscription();
Console.WriteLine(subscription.Id);
It's possible to get a specific subscription as follows:
string subscriptionId = "your-subscription-id";
ArmClient client = new ArmClient(new DefaultAzureCredential());
SubscriptionCollection subscriptions = client.GetSubscriptions();
SubscriptionResource subscription = subscriptions.Get(subscriptionId);
Console.WriteLine($"Got subscription: {subscription.Data.DisplayName}");
You can also specify the default subscription when creating the ArmClient:
string defaultSubscriptionId = "your-subscription-id";
ArmClient client = new ArmClient(new DefaultAzureCredential(), defaultSubscriptionId);
SubscriptionResource subscription = client.GetDefaultSubscription();
Console.WriteLine(subscription.Id);
From here, it is possible to get the resource groups from the retrieved subscription:
ResourceGroupCollection resourceGroups = subscription.GetResourceGroups();
Take a look at the Managing Resource Groups samples.