none
Using SetValue with RegistryValueKind.MultiString

    Question

  • I'm trying to duplicate the behavior of a registry setting in a reg file. The line is as follows:

    [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\RpcLocator]
    "DependOnGroup"=hex(7):00,00

    The "DependOnGroup" value is stored as a REG_MULTI_SZ in the registry. As far as I can tell, the line above should be setting the value to "00 00" in hex ...?

    In my application I'm attempting to duplicate it with very little success. When the above is executed it just sets the registry value to an empty value. Is that possible in C#? I know I could set it to an empty value using something other than RegistryValueKind.MultiString, but then it changes the value type in the registry.
    Wednesday, October 28, 2009 6:26 PM

Answers

  • You are still creating an array with 1 empty item. You should do this:

    key.SetValue("SomeValueName", new string[] {}, RegistryValueKind.MultiString);


    Then, you will have an empty array (which is what you want).


    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
    • Marked as answer by Roahn Luo Tuesday, November 03, 2009 7:24 AM
    Saturday, October 31, 2009 4:02 PM

All replies

  • See this article.

    Short copy/paste for reference only:

    string[] ConfigurationString = new string[8];
    ConfigurationString[0] = Location;
    ConfigurationString[1] = LocalCalls;
    ConfigurationString[2] = LongDistanceCalls;
    ConfigurationString[3] = InternationalCalls;
    ConfigurationString[4] = AreaCode;
    ConfigurationString[5] = DisableCallWaitingSequence;
    ConfigurationString[6] = CountryCode;
    ConfigurationString[7] = ToneOrPulse;
    
    RegistryKey rk = Registry.CurrentUser;
    using (RegistryKey rk1 = rk.CreateSubKey("ControlPanel\
    \Dial\\Locations"))
    {
        rk1.SetValue(KeyName, ConfigurationString);
        rk1.Close();
    }
    

    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
    Thursday, October 29, 2009 12:13 PM
  • I'll try and explain this better...

    I need to set the value of a registry value to nothing (blank) while still maintaining the type REG_MULTI_SZ. Every attempt I've tried so far results in at least SOME sort of character being written as the value, whether its printable or non-printable.

    For example...

    key.SetValue("SomeValueName", "", RegistryValueKind.MultiString);

    ...will crash (I know why its crashing). So I attempted...

    key.SetValue("SomeValueName", new string[] {""}, RegistryValueKind.MultiString);

    ... which runs, but still appends a non-printable character to the blank string value (I'm guessing '\0').

    Could anyone point me in the right direction with this? Is what I'm attempting to do possible, or will I just have to live with the second example I posted?

    Thanks!

    Friday, October 30, 2009 8:43 PM
  • You are still creating an array with 1 empty item. You should do this:

    key.SetValue("SomeValueName", new string[] {}, RegistryValueKind.MultiString);


    Then, you will have an empty array (which is what you want).


    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
    • Marked as answer by Roahn Luo Tuesday, November 03, 2009 7:24 AM
    Saturday, October 31, 2009 4:02 PM
  • Thanks very much, this helped me finding the solution finally!

    Editing settings by registry doesn't apply them immediately, but in combination with WMI you can get it managed to do so. Have look on the stackoverflow article for my final solution.

    Best regards!

    Friday, August 10, 2012 5:12 PM