Click here to Skip to main content
15,887,343 members
Articles / Programming Languages / C# 4.0
Tip/Trick

Setting Null Binary Values In The Registry With C#

Rate me:
Please Sign up or sign in to vote.
4.94/5 (9 votes)
13 Oct 2014CPOL 12.7K   9   1
How to set a null binary value (zero-length binary value) in the registry from your C# program.

Introduction

If you've ever tried to insert a null byte array or binary value programmatically into the Registry using the SetValue method available in the Registry functions provided in the Microsoft.Win32 namespace, you triggered an ArgumentNullException. However, it is possible to create an empty binary value or clear an existing one when using Registry Editor. Here's a simple way to do the same thing from your C# program.

Using the code

The key to this tip is C#'s null-coalescing operator. The ?? operator returns the left operand if it is not null. Otherwise, it returns the right operand. In the sample code below, I'm calling an encryption function for a password. This works fine for non-null passwords, but sometimes a password is not needed, in which case the encryption function returns null. Using ?? combined with an empty byte array successfully sets the value.

Registry Editor refers to a null byte value as a zero-length binary value. The code snippet below shows how to set a zero-length binary value with C# 2.0 or higher code.

// Store the password to the Registry. The Credentials argument to OpenSubKey contains the path
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(Credentials, true))
{
 // Encrypt the password and save it. Set it to empty binary if no password. PasswordTextBox
 // in this case was a WPF PasswordBox control. Encrypt is my AES encryption function, which
 // returns either an encrypted byte array or null if the password is empty

 byte[] encrypted = Encrypt(PasswordTextBox.Password);

 key.SetValue("Password", encrypted ?? new byte[]{ }, RegistryValueKind.Binary);
}

 

History

Original tip submitted 10/13/2014.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Choycer
United States United States
Ed has over 40 years experience in computer technology and a bachelor's degree in Business Administration. He's currently a marketing technology consultant. During his career, he's led software development departments and created software still in use in the communications and healthcare industries. Ed is a veteran of the United States Army. He lives in Arizona in the United States.

Find Ed on Linkedin.

This material is copyright 2019 by Ed Gadziemski. Unauthorized use is strictly prohibited. All rights reserved.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun13-Oct-14 17:02
Humayun Kabir Mamun13-Oct-14 17:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.