• 0

[C#] Reading Registry Values


Question

Here's the situation. I have to iterate through an array of registry values in a Microsoft.Win32.RegistryKey and handle each value differently based on it's type. Here's the values and their data types:

Binary - byte[]

DWORD - int/uint

Expandable string - string

Multi-line string - string[]

String - string

I have everything working except for the expandable strings. Since they share a type with normal string values, a problem arises. I can't tell which type a value is supposed to be when I'm looping through an array of values. Here's my code:

foreach(string valueName in registryKey.GetValueNames())
{
 ? ?byte[] binaryTest = registryKey.GetValue(valueName) as byte[];
 ? ?if(binaryTest != null)
 ? ?{
 ? ? ? ?// handle binary value
 ? ? ? ?continue;
 ? ?}
 ? ?string[] multiTest = registryKey.GetValue(valueName) as string[];
 ? ?if(multiTest != null)
 ? ?{
 ? ? ? ?// handle multi-string value
 ? ? ? ?continue;
 ? ?}
 ? ?string stringTest = registryKey.GetValue(valueName) as string;
 ? ?if(stringTest != null)
 ? ?{
 ? ? ? ?if(stringTest.IndexOf("%") != stringTest.LastIndexOf("%"))
 ? ? ? ?{
 ? ? ? ? ? ?// handle expandable string value
 ? ?     ? ?continue;
 ? ? ? ?}
 ? ? ? ?else
 ? ? ? ?{
 ? ? ? ? ? ?// handle normal string value
 ? ?     ? ?continue;
 ? ? ? ?}
 ? ?}

As you can see, when I test for a string value, I'm trying to check for "%" because that would signify an expandable string. The problem is that expandable strings are meant to be unexpanded automa%ProgramFiles%m the registry. So when I try anC:\Program Filesot;%ProgramFiles%" for example, I get "C:\Program:huh:s" instead... How can I tell which strings are which types of values? :huh:

Link to comment
https://www.neowin.net/forum/topic/239805-c-reading-registry-values/
Share on other sites

1 answer to this question

Recommended Posts

  • 0

From MSDN:

REG_EXPAND_SZ: Null-terminated string that contains unexpanded references to environment variables (for example, "%PATH%"). It will be a Unicode or ANSI string depending on whether you use the Unicode or ANSI functions. To expand the environment variable references, use the ExpandEnvironmentStrings function.

This doesn't really help you if .NET auto expands these strings.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.