473,385 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Writing MULTI_SZ to registry

Bry
Having mastered reading a multi_sz from the registry into an array
using:

// Max of 999 is fine in this instance
string[] myArray = new string[999];
myArray = (string[]) (myRegKey.GetValue("RegValue"));
// myArray will now contain a small number of elements with data in
them

I'm trying to do the opposite and write it back to the registry using:

// I may have added a few elements to the array since
// it was loaded from the registry in the above example.
myRegKey.SetValue("RegValue", myArray, RegistryValueKind.MultiString);

but the last line gives a "RegistryKey.SetValue does not allow a
String[] that contains a null String reference." exception at run time.

The exception occurs because only the first few elements of the array
contain real data, the exception is thrown because the other elements
all contain 'null'.

How can I write only the used elements to the multi_sz registry value?
Note that the array will never contain a fixed number of elements, but
the max of 999 will never be achieved (although I have allowed for this
occuring in my code).

Thanks.

Nov 17 '05 #1
3 5391
Bry
I've found a way around this. My data type when loaded from the
registry is an array, but I now convert this to an arraylist which
allows better manipulation throughout my code. When I want to write the
data back to the registry I need to convert the arraylist back to a
fixed size array.

string[] myArray = new string[myArrayList.Count];
for (int loop = 0; loop < myArrayList.Count; loop++)
{
myArray[loop] = (string) myArrayList[loop];
}
regKey.SetValue("myMultiSZValue", myArray,
RegistryValueKind.MultiString);

I'm sure there is a much easier way to do this, but hopefully this
might help someone else with the same problem.

Nov 17 '05 #2
Hi Bry,

Question: Do you know the # of real elements in the array? If so, you can
create a new array that contains only that # of elements and copy the
contents into this new array. If not, you can determine this amount with a
loop (i.e. loop until you reach null and count up with an accumulator), but
that slows down performance.

You can also try initializing your array to string.Empty, or replacing the
nulls with String.Empty, etc. But you need to evaluate if that's the
behavior you want or if the perf hit associated with doing that is
acceptable.

--

Joshua Mitts
jo****@msn.com

"Bry" <br*********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Having mastered reading a multi_sz from the registry into an array
using:

// Max of 999 is fine in this instance
string[] myArray = new string[999];
myArray = (string[]) (myRegKey.GetValue("RegValue"));
// myArray will now contain a small number of elements with data in
them

I'm trying to do the opposite and write it back to the registry using:

// I may have added a few elements to the array since
// it was loaded from the registry in the above example.
myRegKey.SetValue("RegValue", myArray, RegistryValueKind.MultiString);

but the last line gives a "RegistryKey.SetValue does not allow a
String[] that contains a null String reference." exception at run time.

The exception occurs because only the first few elements of the array
contain real data, the exception is thrown because the other elements
all contain 'null'.

How can I write only the used elements to the multi_sz registry value?
Note that the array will never contain a fixed number of elements, but
the max of 999 will never be achieved (although I have allowed for this
occuring in my code).

Thanks.

Nov 17 '05 #3
Bry
Thanks Joshua, that's kind of what I've done (you posted at the same
time as my previous message).

The string.empty idea is interesting, I might give that a go at a later
date. The arraylist conversion allows me much more control throughout
the rest of my code now, however I'm sure I can optimise the code that
performs the conversion a little better than my first attempt.

Thanks for your help.
Bry.

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Pat Blair | last post by:
Sorry to anyone who read this post, but in case it's useful to anyone: Further experiments reveal that while a tuple comes back if you read a multi-line string, you set the value using a list (not...
6
by: msnews.microsoft.com | last post by:
Hi all, I was trying to find an easy way to write in the standard configuration (like app.config) xml-based files. I found classes that can be used to read this information (in...
2
by: glenn | last post by:
I am trying to write to the registry and the objects that are suppose to be handling this function are not working. Here is what is happening. I am issuing the following commands: RegistryKey...
6
by: Bry | last post by:
I'm having problems writing (and reading) boolean data to the registry. // Write a boolean value to the registry // I've not included the obvious bits of code in these samples bool myBool =...
5
by: UJ | last post by:
I have a system that has five programs that all communicate with each other via Message Queues. Works well. One program is a watchdog that will make sure the others are up and going. Currently I...
3
by: Sreppohcdoow | last post by:
I have a simple tool that I create that stores some settings in the Registry... however, if the user running it doesn't have Admin priveleges on the machine, the app fails when trying to write to...
0
by: Chris Mullins | last post by:
I have an application that installs some 64-bit binaries for development use in Visual Studio 2005. As such I want them to appear in the .Net References menu when someone attempts to "Add...
1
by: Chris Mullins | last post by:
I need to write to the 32-bit registry, and need to do so from a 64-bit MSI. It never occurred to me that this would be difficult... I have an application that installs some 64-bit binaries for...
1
by: projectVBA | last post by:
Hi , I wrote some add in for PowerPoint and i'm trying to find an automatic way to load it every time PowerPoint starts. I found Microsoft Code example (see below ) BUT the problem is : I can't...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.