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

Discussion in 'Microsoft C# .NET' started by Guest, Nov 10, 2004.

  1. Guest

    Guest 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.
     
    Guest, Nov 10, 2004
    #1
    1. Advertisements

  2. 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 [C# MVP], Nov 11, 2004
    #2
    1. Advertisements

  3. Guest

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

     
    Guest, Nov 11, 2004
    #3
  4. 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 [C# MVP], Nov 12, 2004
    #4
  5. Guest

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

    ==
     
    Guest, Nov 12, 2004
    #5
  6. Guest

    Guest 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())


     
    Guest, Nov 12, 2004
    #6
  7. 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 [C# MVP], Nov 12, 2004
    #7
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.