2

I'm a BATCH fan and I am trying to implement my key using C# winForms instead of batch.

I made a simple batch Script:

@echo off
reg add "HKEY_CURRENT_USER\Software\TestProgram\Kool" /f /v "Setting" /t REG_BINARY /d h4h999d27e93f3666188231a018c9d44406136wb 1>nul 2>&1

Which updates my reg key no worries whatsoever. However I want to try and implement the same result with C# WinForms and I believe I have the correct formula but the result is not correct.

I tried the following(after checking if the reg_binary exists first and deleting it first):

    private void button1_Click(object sender, EventArgs e)
    {
        if (Registry.GetValue(@"HKEY_CURRENT_USER\Software\TestProgram\Kool", "Setting", null) == null)
        {
            RegistryKey key = Registry.CurrentUser.CreateSubKey(@"Software\TestProgram\Kool");
            try
            {
                var data = Encoding.Unicode.GetBytes("h4h999d27e93f3666188231a018c9d44406136wb");
                //Storing the values
                key.SetValue("Setting", data, RegistryValueKind.Binary);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
                MessageBox.Show(exc.StackTrace);
            }
            key.Close();

            MessageBox.Show("binary key created");
        }
    else
        {
             MessageBox.Show("error");
        }
   }

Which makes the key no worries but its not the same format or result as what I get when i do it through batch, can anyone explain to me why? I am after the same result as when I do it with my batch file.

I feel like I am missing something simple maybe its incorrect bytes I just can't seem to work it out.

I have read these for help: Write a Stringformated Hex Block to registry in Binary value How to retrieve a REG_BINARY value from registry and convert to string How can I read binary data from registry to byte array Read Registry_binary and convert to string

But still no success.

Burgo855
  • 248
  • 1
  • 5
  • 19

1 Answers1

3

To create a binary key, you need to pass a byte[]. So you should first convert the hex string to byte array, then add the value.

For example:

var path = @"Software\TestProgram\Kool";
var key = Registry.CurrentUser.OpenSubKey(path, true);
if (key == null)
    key = Registry.CurrentUser.CreateSubKey(path, true);
var hex = "d5d000d27e93f3116100224a018c9d00406136c3";
var value = StringToByteArray(hex);
key.SetValue("Setting", value, RegistryValueKind.Binary);

I used StringToByteArray method which JaredPar has shared in this post:

public static byte[] StringToByteArray(string hex) {
    return Enumerable.Range(0, hex.Length)
                     .Where(x => x % 2 == 0)
                     .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                     .ToArray();
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • So, is there any reason as to why in BATCH it works without having to convert the hex value? Its like batch automatically converts the hex to a byte. – Burgo855 Jun 11 '18 at 07:03
  • 1
    `d5d000d27e93f3116100224a018c9d00406136c3` will be treated as a hex value by the reg add command. But here is C# you need to pass it as `byte[]`. – Reza Aghaei Jun 11 '18 at 07:07