Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add new string to Registry Entry of type REG_MULTI_SZ?

I have 1 registry entry of type REG_MULTI_SZ. This entry already contains some string in it.

Now i want to add 1 more string to it by using .net classRegistryKey . this class has method key.SetValue(string,string). But when i use this method it removes all strings which are already there and then inserts new string, in short it overwrites. I don't want to touch strings which are already there, just want to add new string at the end. Anybody know how can we do this in C#.

like image 446
sagar Avatar asked Jul 24 '12 11:07

sagar


People also ask

How do you create a new string in regedit?

Creating a string value is similar to creating a key – right-click on the key where you want to create the string value, hover on New in the menu, and then select String Value from the submenu.

How do I add to a registry key?

Once you've located the registry key you want to add to, you can add the key or value you want to add: If you're creating a new registry key, right-click or tap-and-hold on the key it should exist under and choose New > Key. Name the new registry key and then press Enter.

How do I change the registry type?

Click Start, click Run, type regedit in the Open box, and then click OK. Locate and then click the subkey that holds the registry item or items that you want to change. Click File, and then click Export. This step backs up the subkey before you make any changes.

What is string value registry?

String Value: String Values are mostly human readable and only have a single line of textual information, like file paths. String Values are one of the most commonly used values in the Windows Registry. Binary Value: As the name implies, these values only contain binary data (0, 1).


3 Answers

For a multi string value I would do this.

key.SetValue("MultipleStringValue", new string[] {"One", "Two", "Three"});

If it's a single value then as Wolfgang mentioned you should read existing and append.

like image 166
Vinayak Kolagi Avatar answered Oct 18 '22 19:10

Vinayak Kolagi


As per Vinayak's answer above, if you want to append a value, you would need to read in the existing value and work with that.

So:

var registryValue = key.GetValue("KeyName");

to get the existing value, which should return a string array for a REG_MULTI_SZ. Then it's just a matter of appending the new value (or swapping out an existing value, if that takes your fancy). You can do this any way you prefer - create a List from the array and add a new value to it, create a new string array with a loop, make use of IEnumerable.Concat...

var stringList = new List<string>(registryValue as string[]);
stringList.Add("NewValue");
key.SetValue("KeyName", stringList.ToArray());

Obviously you'd want to put some defensive coding around this little sample - make sure you're actually getting back a string array, etc...

like image 27
Dan Shane Avatar answered Oct 18 '22 19:10

Dan Shane


There is no other way. You have to read the existing string first, then append your string to that and write the new concatenated string back.

like image 38
Wolfgang Ziegler Avatar answered Oct 18 '22 21:10

Wolfgang Ziegler