예제 #1
0
        public async Task CheckIfResourceGroupExists()
        {
            #region Snippet:Readme_ExistsRG
            ArmClient client = new ArmClient(new DefaultAzureCredential());
            Subscription subscription = await client.GetDefaultSubscriptionAsync();
            ResourceGroupCollection resourceGroups = subscription.GetResourceGroups();
            string resourceGroupName = "myRgName";

            bool exists = await resourceGroups.ExistsAsync(resourceGroupName);

            if (exists)
            {
                Console.WriteLine($"Resource Group {resourceGroupName} exists.");

                // We can get the resource group now that we know it exists.
                // This does introduce a small race condition where resource group could have been deleted between the check and the get.
                ResourceGroup resourceGroup = await resourceGroups.GetAsync(resourceGroupName);
            }
            else
            {
                Console.WriteLine($"Resource Group {resourceGroupName} does not exist.");
            }
            #endregion Snippet:Readme_ExistsRG
        }