Share via


方法 : Windows レジストリにデータを書き込む

更新 : 2007 年 11 月

CurrentUser キーを使用して、Software キーに対応する RegistryKey クラスの書き込み可能インスタンスを作成する方法を次のコード例に示します。その後、CreateSubKey メソッドを使用して、新しいキーを作成し、キーと値のペアを追加します。

コード

// registry_write.cpp
// compile with: /clr
using namespace System;
using namespace Microsoft::Win32;

int main()
{
   // The second OpenSubKey argument indicates that
   // the subkey should be writable. 
   RegistryKey^ rk;
   rk  = Registry::CurrentUser->OpenSubKey("Software", true);
   if (!rk)
   {
      Console::WriteLine("Failed to open CurrentUser/Software key");
      return -1;
   }

   RegistryKey^ nk = rk->CreateSubKey("NewRegKey");
   if (!nk)
   {
      Console::WriteLine("Failed to create 'NewRegKey'");
      return -1;
   }

   String^ newValue = "NewValue";
   try
   {
      nk->SetValue("NewKey", newValue);
      nk->SetValue("NewKey2", 44);
   }
   catch (Exception^)
   {
      Console::WriteLine("Failed to set new values in 'NewRegKey'");
      return -1;
   }

   Console::WriteLine("New key created.");
   Console::Write("Use REGEDIT.EXE to verify ");
   Console::WriteLine("'CURRENTUSER/Software/NewRegKey'\n");
   return 0;
}

解説

.NET Framework を使用すると、Registry クラスと RegistryKey クラスを使用してレジストリにアクセスできます。この 2 つのクラスはどちらも Microsoft.Win32 名前空間で定義されています。Registry クラスは、RegistryKey クラスの静的インスタンスのコンテナです。各インスタンスは、ルート レジストリ ノードを表します。インスタンスは、ClassesRootCurrentConfigCurrentUserLocalMachine、および Users です。

参照

概念

方法 : Windows レジストリからデータを読み込む

その他の技術情報

.NET プログラミング ガイド