PRB: RegistryKey.SetValue Method Stores Integer Values as Strings (317873)



The information in this article applies to:

  • Microsoft .NET Framework Class Libraries 1.0
  • Microsoft .NET Framework Class Libraries 1.1

This article was previously published under Q317873
IMPORTANT: This article contains information about modifying the registry. Before you modify the registry, make sure to back it up and make sure that you understand how to restore the registry if a problem occurs. For information about how to back up, restore, and edit the registry, click the following article number to view the article in the Microsoft Knowledge Base:

256986 Description of the Microsoft Windows Registry

SYMPTOMS

The RegistryKey class in the Microsoft.Win32 namespace contains a method called SetValue. This method is used to store values in the system registry. The first parameter in the SetValue method is a string that specifies the name of the value. The second parameter is an object that contains the data to be stored. The object type determines the registry type that will be used to store the value.

If the second parameter in SetValue is an Int32 value, the value is stored as an integer with the REG_DWORD registry value type. However, if the second parameter is any other type of integer (such as Int16, UInt16, UInt32, and so on), the value is stored as a string with the REG_SZ registry value type.

RESOLUTION

To store a non-Int32 integer value in the registry as a REG_DWORD value, typecast the non-Int32 integer value to an Int32 value.

Sample Code

Before you edit the registry, read the "Add and Delete Information in the Registry" and "Edit Registry Data" Help topics in Regedt32.exe. Note that you should back up the registry before you edit it. If you are running Microsoft Windows NT or Microsoft Windows 2000, you should also update your Emergency Repair Disk (ERD).

Use the following sample Visual C# code to store a UInt32 value in the registry as a REG_DWORD value:
using System;
using Microsoft.Win32;

namespace MyNamespace {

   class MyClass {

      static void Main() {

         UInt32 Val = UInt32.MaxValue;
         RegistryKey MyKey = 
               Registry.CurrentUser.CreateSubKey("SOFTWARE\\MyApp");
         MyKey.SetValue("MyVal_String", Val);         // store as REG_SZ
         MyKey.SetValue("MyVal_DWORD", (Int32) Val);  // store as REG_DWORD
      }

   }

}
				
Use the following sample code for Visual C++:
#using <mscorlib.dll>

using namespace System;
using namespace Microsoft::Win32;

int main(void) {

   UInt32 Val = UInt32::MaxValue;
   RegistryKey *MyKey = 
      Registry::CurrentUser->CreateSubKey("SOFTWARE\\MyApp");
   MyKey->SetValue(S"MyVal_String", __box(Val));         // store as REG_SZ
   MyKey->SetValue(S"MyVal_DWORD", __box((Int32) Val));  // store as REG_DWORD
   return 0;
}
				

STATUS

This behavior is by design.

Modification Type:MajorLast Reviewed:10/22/2003
Keywords:kbKernBase kbprb kbRegistry KB317873 kbAudDeveloper