473,322 Members | 1,614 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,322 software developers and data experts.

Read registry keys of type REG_MULTI_SZ into C# arrays

Bo
I am using RegistryKey.GetValue() method to retrieve values from
Windows Registry. I don't know how to read type REG_MULTI_SZ into a
string array. I have tried
string[] array = (string[]) RKey.GetValue(name);
But it gave me "Invalid Cast Exception".

Please provide C# sample code in your reply.

Thank you.
Nov 15 '05 #1
3 17643
Excerpt from MSDN:

Note When setting a value, the way in which the value being passed is
stored in the registry is interpreted. There is no way to control whether
the information being passed is stored as an sz, or an expanded_sz string,
and therefore, all string values are interpreted as standard sz values.
I guess if you extend this to read, you'll be able to read MULTI_SZ strings
as one string... although you should try a sample app to make sure.

-vJ
"Bo" <bo*****@yahoo.com> wrote in message
news:a7**************************@posting.google.c om...
I am using RegistryKey.GetValue() method to retrieve values from
Windows Registry. I don't know how to read type REG_MULTI_SZ into a
string array. I have tried
string[] array = (string[]) RKey.GetValue(name);
But it gave me "Invalid Cast Exception".

Please provide C# sample code in your reply.

Thank you.

Nov 15 '05 #2

Hi Bo,

It should work, I do like this:
private void button1_Click(object sender, System.EventArgs e)
{
string[] before = new string[] {"Hello", "World"};
string[] after;
RegistryKey key = Registry.CurrentUser.CreateSubKey(@"Software\GetVa lue");
key.SetValue("Test", before);
after = (string[])key.GetValue("Test");
foreach(string str in after)
{
MessageBox.Show(str);
}
key.Close();
}

And it works well.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #3
Bo wrote:
I am using RegistryKey.GetValue() method to retrieve values from
Windows Registry. I don't know how to read type REG_MULTI_SZ into a
string array. I have tried
string[] array = (string[]) RKey.GetValue(name);
But it gave me "Invalid Cast Exception".


Something else is wrong - it works fine for me. I suspect that the value
being read by RKey.GetValue(name) is not really REG_MULTI_SZ.

I'll even post a complete example:

using System;
using Microsoft.Win32;

namespace MultiSZTest
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string [] vals = (string [])
Registry.CurrentUser.GetValue( "test");
}
}
}

Note that I created a REG_MULTI_SZ registry value named "test" under
HKCU using RegEdit. Note that my test worked even if the value had no
data in it or just one string.

I also got no exception if the value "test" did not exist at all. In
that case, the vals array was null.

I did get the InvalidCastException (as expected) if "test" had a type of
REG_SZ.

Now you get to post a complete example that fails.

--
mikeb
Nov 15 '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...
1
by: Jim Moon | last post by:
I'm setting up a Win Server 2003 for remote debugging. From this page, I see that I need to set two registry keys on the server:...
1
by: Borka | last post by:
Hi all! I have some questions. 1. I write script which controls registry settings, which must work on every computer. If I try JFSO = new ActiveXObject("Scripting.FileSystemObject"); JWS =...
0
by: DJP | last post by:
Hi there I need to be able to programmatically set permissions on registry keys using VB / VBScript / VBA. So far I have had a look at doing this using the WScript.Shell object, API calls, and...
0
by: sdb1031 | last post by:
Hi, I am trying to learn how to write, change and delete registry keys and values of a remote computer via Python's _winreg module. So far, I've been able to programmatically create a value and...
2
by: jphelan | last post by:
I was testing the install and uninstall, on my WindowsXP Pro SP2 machine, of software that I created, called, "inbusiness". Because of the following error message that I received on attempting to...
5
by: Sin Jeong-hun | last post by:
I need to read some registry keys of a remote computer. The key will be any key users provide. (For example : HKEY_CURRENT_USER\SOFTWARE \MyGame) I found that there was a handy method called...
0
by: Gary | last post by:
I'm having trouble entering Additional Registry Keys into my Package Solution that will deploy the Access 2007 runtime. (Client machine is running XP with Office 2007 Standard.) The Package...
0
by: PRR | last post by:
i want to add/modify HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\SysmonLog\Log Queries i might warn u guys .. i practically ..dunno much abt registry .. I want to define my own...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.