tone7
tone7

Reputation: 365

Byte[] from Registry returns only one letter

I'm trying to read data from the registry @ ""SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\RecentDocs\"

The return value I get is System.byte[], when I convert it to a string like suggested here.

It works (I think). But I only get 1 letter returned and not the whole string.

Perhaps I'm doing something wrong? I'm fairly certain there can't be only one letter in there..

I've tried Encoding.ASCII.GetString(bytes); and Encoding.UTF8.GetString(bytes); and Encoding.Default.GetString(bytes); but it all returns only 1 character/letter.

I've checkout this link as well. But thats for C++ and I'm using C# and don't see that Method that they suggested (RegGetValueA)

Here is my code:

        RegistryKey pRegKey = Registry.CurrentUser;
        pRegKey = pRegKey.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RecentDocs\\");
        Object val = pRegKey..GetValue("0");

        byte[] bytes = (byte[])pRegKey.GetValue ("0");
        string str = Encoding.ASCII.GetString(bytes);

        System.Windows.MessageBox.Show("The value is: " + str);

Thanks in advance for any help :)

Upvotes: 2

Views: 530

Answers (2)

svick
svick

Reputation: 245076

The string is encoded using UTF-16, so you should use Encoding.Unicode.

But it doesn't seem it's just UTF-16 encoded strings, there's some more data. For me, (when decoded as UTF-16), it displays as

Stažené soubory□Š6□□□□□Stažené soubory.lnk□T□□뻯□□□□*□□□□□□□□□□□□Stažené soubory.lnk□6□

Stažené soubory means Downloads in Czech, which is the language of my Windows. And the U+25A1 squares in the above text are actually zero chars.

Upvotes: 3

Yahia
Yahia

Reputation: 70369

Are you sure that the encoding is ASCII ?

I would suspect some UTF like Encoding.UTF8 or Encoding.Unicode - try that...

Upvotes: 2

Related Questions