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

G

Guest

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.
 
J

Jon Skeet [C# MVP]

auldh said:
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.
 
G

Guest

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 said:
auldh said:
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.
 
J

Jon Skeet [C# MVP]

auldh said:
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)
{
...
}
}
 
G

Guest

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 said:
auldh said:
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)
{
...
}
}
 
G

Guest

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 said:
auldh said:
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)
{
...
}
}
 
J

Jon Skeet [C# MVP]

auldh said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top