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

how to read REG_MULTI_SZ and REG_SZ types in the registry with C#

so far i have been able to understand how to use C# to read the regisry but
getting to read the MULTI_SZ and REG_SZ just doesn't make sense.

my sample program outputs the data as "System.String[]" the MSDN is not
clear on point as to how to display the data. any help will be grateful.
Nov 16 '05 #1
6 14394
auldh <au***@discussions.microsoft.com> wrote:
so far i have been able to understand how to use C# to read the regisry but
getting to read the MULTI_SZ and REG_SZ just doesn't make sense.

my sample program outputs the data as "System.String[]" the MSDN is not
clear on point as to how to display the data. any help will be grateful.


Your sample program is no doubt just calling ToString (directly or
indirectly), and the fact that it outputs "System.String[]" just means
that the returned value was a string array. Cast it to string[] and
look at the elements of the array.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Jon, i'm a novice to C# i have taken most of this code from MSDN Registry
Class section. i'm trying out C# over my VB6.0.
i believe i understand what you are saying but i think the problem is
knowing when to look at the value as an array. in most cases the exiting line
of code

foreach(string valueName in tempKey.GetValueNames())
{
Console.WriteLine("{0,-8}: {1} {2}", valueName,
tempKey.GetValue(valueName).ToString(),
tempKey.GetValue(valueName).GetType());
}//foreach(string valueName in tempKey.GetValueNames())

gives the output correctly. in VB6.0 i used a class the recoginized the when
to expand the data as an array. in the MSDN i don't see how it is done. from
what you have written i not sure it is easy for me to do read.

"Jon Skeet [C# MVP]" wrote:
auldh <au***@discussions.microsoft.com> wrote:
so far i have been able to understand how to use C# to read the regisry but
getting to read the MULTI_SZ and REG_SZ just doesn't make sense.

my sample program outputs the data as "System.String[]" the MSDN is not
clear on point as to how to display the data. any help will be grateful.


Your sample program is no doubt just calling ToString (directly or
indirectly), and the fact that it outputs "System.String[]" just means
that the returned value was a string array. Cast it to string[] and
look at the elements of the array.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #3
auldh <au***@discussions.microsoft.com> wrote:
Jon, i'm a novice to C# i have taken most of this code from MSDN Registry
Class section. i'm trying out C# over my VB6.0.
i believe i understand what you are saying but i think the problem is
knowing when to look at the value as an array. in most cases the exiting line
of code

foreach(string valueName in tempKey.GetValueNames())
{
Console.WriteLine("{0,-8}: {1} {2}", valueName,
tempKey.GetValue(valueName).ToString(),
tempKey.GetValue(valueName).GetType());
}//foreach(string valueName in tempKey.GetValueNames())

gives the output correctly. in VB6.0 i used a class the recoginized the when
to expand the data as an array. in the MSDN i don't see how it is done. from
what you have written i not sure it is easy for me to do read.


If you get the value as an object, you can test its type:

object o = tempKey.GetValue(valueName);

string stringValue = o as string;
if (stringValue != null)
{
// it's a string
}
else
{
string[] stringArray = o as string[];
if (stringArray != null)
{
...
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
thank you. so far C# is much more difficult than i image. its too bad i could
not just cast the
object o = tempKey.GetValue(valueName);
string stringValue = o as string;
as an array up front thus removing more code than i wanted. i'm adding the
changes so you can comment if there is any better way to handle it since i'm
not comfortable with C#.
==
foreach(string valueName in tempKey.GetValueNames())
{
object o = tempKey.GetValue(valueName);
string stringValue = o as string;
//test for string
if (stringValue != null)
{
//if string print
Console.WriteLine("{0,-8}: {1} \t\t\t{2}", valueName,
tempKey.GetValue(valueName).ToString(),
tempKey.GetValue(valueName).GetType());
}
else
{
//test for array again
string[] stringArray = o as string[];
int i= 0;
if (stringArray != null)
{
//if an array iterate and print elements!
System.Collections.IEnumerator myEnumerator = stringArray.GetEnumerator();
Console.WriteLine("{0,-8}: ", valueName);
Console.WriteLine( "\tis an Array containing the following values:" );
while (( myEnumerator.MoveNext() ) && ( myEnumerator.Current != null ))
Console.WriteLine( "\t\t[{0}] {1}", i++, myEnumerator.Current );

}
else
{
//if not string or an array print as usual
Console.WriteLine("{0,-8}: {1} \t\t\t{2}", valueName,
tempKey.GetValue(valueName).ToString(),
tempKey.GetValue(valueName).GetType());
}
}
Console.WriteLine("{0,-8}: {1} {2}", valueName,
tempKey.GetValue(valueName).ToString(),
tempKey.GetValue(valueName).GetType()); */
}//foreach(string valueName in tempKey.GetValueNames())

thank you for help on this any revisions would help greatly.

==
"Jon Skeet [C# MVP]" wrote:
auldh <au***@discussions.microsoft.com> wrote:
Jon, i'm a novice to C# i have taken most of this code from MSDN Registry
Class section. i'm trying out C# over my VB6.0.
i believe i understand what you are saying but i think the problem is
knowing when to look at the value as an array. in most cases the exiting line
of code

foreach(string valueName in tempKey.GetValueNames())
{
Console.WriteLine("{0,-8}: {1} {2}", valueName,
tempKey.GetValue(valueName).ToString(),
tempKey.GetValue(valueName).GetType());
}//foreach(string valueName in tempKey.GetValueNames())

gives the output correctly. in VB6.0 i used a class the recoginized the when
to expand the data as an array. in the MSDN i don't see how it is done. from
what you have written i not sure it is easy for me to do read.


If you get the value as an object, you can test its type:

object o = tempKey.GetValue(valueName);

string stringValue = o as string;
if (stringValue != null)
{
// it's a string
}
else
{
string[] stringArray = o as string[];
if (stringArray != null)
{
...
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
thank you very much. it is a shame i can not cast right away to
an array thus making the code much more cleaner. but being a newbee
i guess i have to grow into this C# language. i'm including my code
incase you can see some improvements.
foreach(string valueName in tempKey.GetValueNames())
{
object o = tempKey.GetValue(valueName);
string stringValue = o as string;
//test for string
if (stringValue != null)
{
//if string print
Console.WriteLine("{0,-8}: {1} \t\t\t{2}", valueName,
tempKey.GetValue(valueName).ToString(),
tempKey.GetValue(valueName).GetType());
}
else
{
//test for array again
string[] stringArray = o as string[];
int i= 0;
if (stringArray != null)
{
//if an array iterate and print elements!
System.Collections.IEnumerator myEnumerator = stringArray.GetEnumerator();
Console.WriteLine("{0,-8}: ", valueName);
Console.WriteLine( "\tis an Array containing the following values:" );
while (( myEnumerator.MoveNext() ) && ( myEnumerator.Current != null ))
Console.WriteLine( "\t\t[{0}] {1}", i++, myEnumerator.Current );

}
else
{
//if not string or an array print as usual
Console.WriteLine("{0,-8}: {1} \t\t\t{2}", valueName,
tempKey.GetValue(valueName).ToString(),
tempKey.GetValue(valueName).GetType());
}
}
Console.WriteLine("{0,-8}: {1} {2}", valueName,
tempKey.GetValue(valueName).ToString(),
tempKey.GetValue(valueName).GetType()); */
}//foreach(string valueName in tempKey.GetValueNames())
"Jon Skeet [C# MVP]" wrote:
auldh <au***@discussions.microsoft.com> wrote:
Jon, i'm a novice to C# i have taken most of this code from MSDN Registry
Class section. i'm trying out C# over my VB6.0.
i believe i understand what you are saying but i think the problem is
knowing when to look at the value as an array. in most cases the exiting line
of code

foreach(string valueName in tempKey.GetValueNames())
{
Console.WriteLine("{0,-8}: {1} {2}", valueName,
tempKey.GetValue(valueName).ToString(),
tempKey.GetValue(valueName).GetType());
}//foreach(string valueName in tempKey.GetValueNames())

gives the output correctly. in VB6.0 i used a class the recoginized the when
to expand the data as an array. in the MSDN i don't see how it is done. from
what you have written i not sure it is easy for me to do read.


If you get the value as an object, you can test its type:

object o = tempKey.GetValue(valueName);

string stringValue = o as string;
if (stringValue != null)
{
// it's a string
}
else
{
string[] stringArray = o as string[];
if (stringArray != null)
{
...
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6
auldh <au***@discussions.microsoft.com> wrote:
thank you. so far C# is much more difficult than i image. its too bad i could
not just cast the
object o = tempKey.GetValue(valueName);
string stringValue = o as string;
as an array up front thus removing more code than i wanted. i'm adding the
changes so you can comment if there is any better way to handle it since i'm
not comfortable with C#.


You *can* cast it to an array if you know it's an array - I thought you
didn't know whether it was a string array or just a string though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

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

Similar topics

1
by: Pat Blair | last post by:
Hello. I'm looking for any information I can get about writing values to keys in the Windows registry when the value type is a multi-string (ie. REG_MULTI_SZ). When I get the value of a...
6
by: George Hester | last post by:
This location has a parasite checker using javascript. It is in a js file called parasite.js. It is freely available. http://www.doxdesk.com/parasite/ He\She is the only one I trust on the...
1
by: ChuckD_Duncan | last post by:
using Microsoft.Win32; RegistryKey key = Registry.LocalMachine; key = key.OpenSubKey("....."); ... key = key.OpenSubKey("PCF", true); double aDoubleValue = 0.25; key.SetValue("testkey",...
3
by: Bo | last post by:
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)...
1
by: Asaf Ganot | last post by:
Hi, I'm looking for a way to retrieve the date type of a registry value using c#. Since I couldn't find a way to do it with 'Microsoft.Win32', I tried to use the good old API call 'RegEnumValue'...
3
by: Slimo | last post by:
Hello, I'm searching some example of code (VB) for reading remote registry subkeys and keys. Thanks
2
by: Steve Bernard | last post by:
Hey there, I was wondering if anyone has been able to write reg_multi_sz multi-string values to the registry from VB.NET. All the registry stuff I find from MS can't differentiate different types...
2
by: =?Utf-8?B?QmFzIFJpam5pZXJzY2U=?= | last post by:
Hello, I'm trying to write: strMulti = new string { @"c:\pagefile.sys", "512", "512" }; with: keyRead.SetValue(regKey, strMulti, regKind); Where reKind is Multistring Unfortunately it ends...
3
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, i have a byte that holds a bunch of data of type REG_MULTI_SZ in UNICODE,. I must convert this byte to a REG_MULTI_SZ in ANSI code. How can i do this,...? TIA,... Regards
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.