HOW TO:將資料寫入至 Windows 登錄 (C++/CLI)
下列程式碼範例會使用 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 類別的登錄,這兩個類別都是在 Microsoft.Win32 命名空間中定義。Registry 類別是 RegistryKey 類別之靜態執行個體的容器。每個執行個體表示一個根目錄登錄節點。這些執行個體為 ClassesRoot、CurrentConfig、CurrentUser、LocalMachine 和 Users。
請參閱
概念
HOW TO:從 Windows 登錄讀取資料 (C++/CLI)