tags:

views:

44

answers:

2

I use the Registry class to manage values in the Registry on Windows Seven in C#.

Registry.GetValue(...);

But, I'm facing a curious behavior : Every time, the returned value is the correct one, but sometimes, it is followed by an unexpected "?"

When I check the Registry, (regedit), the "?" doesn't exist. I really don't understand from where this question mark come from.

Info :

  • C#
  • 3.5 framework
  • windows 7 64 bits (and i want my application to work on both 32 and 64 bits systems)
A: 

So my question is, "who set the value"?

Perhaps whoever did the setting put in an unprintable character at the end of the string. It is probably not actually a question mark. This may be a result of a bug in the program which did the setting, not anything to do with your code, per se.

Dave Markle
Here are some answers :The key has been set by me on "HKEY_CURRENT_USER\Software\MySoft\"I set a value that corresponds to a file path (keyname : "path", value "C:\....\MyFile.txt"When I get the value, I just want to get this filePath.I made a test, that only get this value and LOG this value.I ran this test a few times (everytime on the same key).Most of time I got the correct filePath ("C:\....\MyFile.txt"), and a few times, "C:\....\MyFile.txt?"
Wilhelm Peraud
A: 

I found a way to remove the unexpected char, thanks to all your comments ;)

 String value = null;
        try
        {
            foreach (Char item in Registry.GetValue(registryKey, key, "").ToString().ToCharArray())
            {
                if (Char.GetUnicodeCategory(item) != System.Globalization.UnicodeCategory.OtherLetter && Char.GetUnicodeCategory(item) != System.Globalization.UnicodeCategory.OtherNotAssigned)
                {
                    value += item;
                }
            }
        }
        catch (Exception ex)
        {
            LOG.Error("Unable to get value of " + key + ex, ex);
        }
        return value;

I made some tests to know what kind of char appears from time to time. It was, just like you said Larry, an unicode problem. I still don't understand why this char appears sometimes.

Wilhelm Peraud