

Versi 4 (V4) dari AWS SDK untuk .NET telah dirilis\$1

Untuk informasi tentang melanggar perubahan dan memigrasi aplikasi Anda, lihat [topik migrasi](https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/net-dg-v4.html).

 [https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/net-dg-v4.html](https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/net-dg-v4.html)

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# Menghapus pasangan kunci
<a name="delete-key-pairs"></a>

Contoh ini menunjukkan kepada Anda cara menggunakan AWS SDK untuk .NET to delete a key pair. Aplikasi ini mengambil nama sebuah key pair. Ini menghapus key pair dan kemudian menampilkan semua pasangan kunci yang tersedia. Jika Anda tidak memberikan argumen baris perintah, aplikasi hanya menampilkan semua pasangan kunci yang tersedia.

Bagian berikut menyediakan cuplikan dari contoh ini. [Kode lengkap untuk contoh](#delete-key-pairs-complete-code) ditampilkan setelah itu, dan dapat dibangun dan dijalankan apa adanya.

**Topics**
+ [Hapus key pair](#delete-key-pairs-create)
+ [Tampilkan pasangan kunci yang tersedia](#delete-key-pairs-display)
+ [Kode lengkap](#delete-key-pairs-complete-code)

## Hapus key pair
<a name="delete-key-pairs-create"></a>

Cuplikan berikut menghapus key pair.

Contoh [di akhir topik ini](#delete-key-pairs-complete-code) menunjukkan cuplikan ini digunakan.

```
    //
    // Method to delete a key pair
    private static async Task DeleteKeyPair(IAmazonEC2 ec2Client, string keyName)
    {
      await ec2Client.DeleteKeyPairAsync(new DeleteKeyPairRequest{
        KeyName = keyName});
      Console.WriteLine($"\nKey pair {keyName} has been deleted (if it existed).");
    }
```

## Tampilkan pasangan kunci yang tersedia
<a name="delete-key-pairs-display"></a>

Cuplikan berikut menampilkan daftar pasangan kunci yang tersedia.

Contoh [di akhir topik ini](#delete-key-pairs-complete-code) menunjukkan cuplikan ini digunakan.

```
    //
    // Method to show the key pairs that are available
    private static async Task EnumerateKeyPairs(IAmazonEC2 ec2Client)
    {
      DescribeKeyPairsResponse response = await ec2Client.DescribeKeyPairsAsync();
      Console.WriteLine("Available key pairs:");
      foreach (KeyPairInfo item in response.KeyPairs)
        Console.WriteLine($"  {item.KeyName}");
    }
```

## Kode lengkap
<a name="delete-key-pairs-complete-code"></a>

Bagian ini menunjukkan referensi yang relevan dan kode lengkap untuk contoh ini.

### Referensi SDK
<a name="w2aac19c15c21c15c13c19b5b1"></a>

NuGet paket:
+ [AWSSDK.EC2](https://www.nuget.org/packages/AWSSDK.EC2)

Elemen pemrograman:
+ [Namespace Amazon.EC2](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/EC2/NEC2.html)

  [EC2Klien Kelas Amazon](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/EC2/TEC2Client.html)
+ [Namespace Amazon.EC2.Model](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/EC2/NEC2Model.html)

  Kelas [https://docs.aws.amazon.com/sdkfornet/v4/apidocs/](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/)

  Kelas [DescribeKeyPairsResponse](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/EC2/TDescribeKeyPairsResponse.html)

  Kelas [KeyPairInfo](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/EC2/TKeyPairInfo.html)

### Kodenya
<a name="w2aac19c15c21c15c13c19b7b1"></a>

```
using System;
using System.Threading.Tasks;
using Amazon.EC2;
using Amazon.EC2.Model;

namespace EC2DeleteKeyPair
{
  class Program
  {
    static async Task Main(string[] args)
    {
      // Create the EC2 client
      var ec2Client = new AmazonEC2Client();

      if(args.Length == 1)
      {
        // Delete a key pair (if it exists)
        await DeleteKeyPair(ec2Client, args[0]);

        // Display the key pairs that are left
        await EnumerateKeyPairs(ec2Client);
      }
      else
      {
        Console.WriteLine("\nUsage: EC2DeleteKeyPair keypair-name");
        Console.WriteLine("  keypair-name - The name of the key pair you want to delete.");
        Console.WriteLine("\nNo arguments specified.");
        Console.Write(
          "Do you want to see a list of the existing key pairs? ((y) or n): ");
        string response = Console.ReadLine();
        if((string.IsNullOrEmpty(response)) || (response.ToLower() == "y"))
          await EnumerateKeyPairs(ec2Client);
      }
    }


    //
    // Method to delete a key pair
    private static async Task DeleteKeyPair(IAmazonEC2 ec2Client, string keyName)
    {
      await ec2Client.DeleteKeyPairAsync(new DeleteKeyPairRequest{
        KeyName = keyName});
      Console.WriteLine($"\nKey pair {keyName} has been deleted (if it existed).");
    }


    //
    // Method to show the key pairs that are available
    private static async Task EnumerateKeyPairs(IAmazonEC2 ec2Client)
    {
      DescribeKeyPairsResponse response = await ec2Client.DescribeKeyPairsAsync();
      Console.WriteLine("Available key pairs:");
      foreach (KeyPairInfo item in response.KeyPairs)
        Console.WriteLine($"  {item.KeyName}");
    }
  }
}
```