Microsoft KB Archive/317873

= PRB: RegistryKey.SetValue Method Stores Integer Values as Strings =

Article ID: 317873

Article Last Modified on 10/22/2003

-

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 &quot;Add and Delete Information in the Registry&quot; and &quot;Edit Registry Data&quot; 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(&quot;SOFTWARE\\MyApp&quot;); MyKey.SetValue(&quot;MyVal_String&quot;, Val);        // store as REG_SZ MyKey.SetValue(&quot;MyVal_DWORD&quot;, (Int32) Val); // store as REG_DWORD }

}

} Use the following sample code for Visual C++:
 * 1) using 

using namespace System; using namespace Microsoft::Win32;

int main(void) {

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



STATUS
This behavior is by design.

Keywords: kbregistry kbkernbase kbprb KB317873

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.