Ghasem
Ghasem

Reputation: 15603

How to get, compare and set a registry key value in hex

I want to get the the state key value from below address in hex

RegistryKey winTrust = Registry.CurrentUser.OpenSubKey(
  @"Software\Microsoft\Windows\CurrentVersion\WinTrust\Trust Providers\Software Publishing", true);

Then compare it with 23c00 value, and if it's not the same, set it to 23c00

string currentKey = winTrust.GetValue("State").ToString();
if (!currentKey.Equals("23c00"))
 {
   winTrust.SetValue("State", "23c00", RegistryValueKind.DWord);
   winTrust.Close();
 }

It's not working as it gets and compare the decimal value.

How can I do it?

Upvotes: 1

Views: 1160

Answers (2)

Ghasem
Ghasem

Reputation: 15603

I found my solution.

// Convert value as a hex in a string variable
string currentKey = Convert.ToInt32(winTrust.GetValue("State").ToString()).ToString("x");
if (!currentKey.Equals("23c00"))
 {
  winTrust.SetValue("State", 0x00023c00, RegistryValueKind.DWord);
  winTrust.Close();
 }

Upvotes: 1

Richard Schneider
Richard Schneider

Reputation: 35477

You need to get and set the value as an integer.

int value = Int32.Parse(winTrust.GetValue("State"));
if (value != 0x23c00)
   winTrust.SetValue("State", 0x23c00, RegistryValueKind.DWord);
winTrust.Close();

Upvotes: 0

Related Questions