简体   繁体   English

c#Registry System.Byte [] to string

[英]c# Registry System.Byte[] to string

I am currently writing a program that will read part of the windows system registry however some of the values of these keys are of type System.Byte[] when i try and decode these values I can produce a string that has some readable characters that makes but mostly the string is jiberish. 我目前正在编写一个程序,它将读取部分Windows系统注册表,但是这些键的一些值是System.Byte []类型,当我尝试解码这些值时,我可以生成一个具有一些可读字符的字符串但主要是字符串是jiberish。 I have tried several encoding types but none seem to produce the correct results. 我尝试了几种编码类型,但似乎都没有产生正确的结果。 I am just wondering if there is anything that is known to fix this. 我只是想知道是否有任何已知的东西可以解决这个问题。 this is the code i have 这是我的代码

public void getMRU()
{
   String mru = @"Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidlMRU";

   RegistryKey rk = Registry.CurrentUser.OpenSubKey(mru);

   foreach (string skName in rk.GetSubKeyNames())
   {
       RegistryKey sk = rk.OpenSubKey(skName);

       System.Text.Encoding enc = System.Text.Encoding.UTF8;
       string myString = enc.GetString((Byte[])sk.GetValue("0"));
       Console.WriteLine(myString)
   }
}

The correct decoding and interpretation varies from key to key. 正确的解码和解释因密钥而异。 For binary values there is no enforced format, applications are free to store any bytes they wish. 对于二进制值,没有强制格式,应用程序可以自由存储他们希望的任何字节。 You must know what you read in order to interpret it. 你必须知道你所读到的内容才能解释它。 If the key content is documented, then you can apply the documentation specifications to decode the content. 如果记录了关键内容,则可以应用文档规范来解码内容。 If is undocumented, then you have no business reading it. 如果没有记录,那么你没有业务阅读它。

It's not guaranteed that the bytes that are stored in the registry for any value are necessarily encoded strings. 无法保证在注册表中存储的任何值的字节都必须是编码字符串。 If they were to be strings, then the type of the registry value would be just that, a string. 如果它们是字符串,那么注册表值的类型就是字符串。

If it is a byte value, then it usually means that it is an encoding of some sort of data structure that is custom to the application, so you have to figure out what the format is for the application which owns the key that you are reading. 如果它是一个字节值,那么它通常意味着它是对应用程序自定义的某种数据结构的编码,因此您必须弄清楚拥有您正在阅读的密钥的应用程序的格式。

From the name of the registry key, I would guess that those blobs aren't encoded filename strings at all, but instead are PIDLs (or rather ITEMIDLISTs). 从注册表项的名称,我猜这些blob根本不是编码的文件名字符串,而是PIDL(或更确切地说是ITEMIDLISTs)。 A PIDL is a shell structure representing an entity in the shell namespace, which need not be a file (eg Control Panel). PIDL是表示shell命名空间中的实体的shell结构,它不必是文件(例如,控制面板)。

If that's the case, you'll probably need to use the SHGetPathFromIDList API to convert the PIDL to a filesystem path: pass in the address of the byte[] array as the pidl parameter. 如果是这种情况,您可能需要使用SHGetPathFromIDList API将PIDL转换为文件系统路径:将byte []数组的地址作为pidl参数传递。

Perhaps this is no longer an issue, but to help anyone else who might encounter this problem consider the following: 也许这不再是一个问题,但为了帮助其他可能遇到此问题的人,请考虑以下事项:

A PIDL, or pointer to an item identifier list only exists in Windows NT 6.x (Vista, Win7) and actually starts at the desktop rather than C:\\... (I believe this was done due to the introduction of "libraries"). PIDL或指向项目标识符列表的指针仅存在于Windows NT 6.x(Vista,Win7)中,实际上是从桌面开始而不是C:\\ ...(我相信这是由于引入了“库”而完成的“)。 Thus, you need special handling to extract a file path from a PIDL. 因此,您需要特殊处理来从PIDL中提取文件路径。

The following method, GetPathFromPIDL(), will take a registry key's (byte[]) byteCode and covert it to a platform specific IntPtr . 以下方法GetPathFromPIDL()将获取注册表项(byte[]) byteCode并将其转换为特定于平台的IntPtr Then we take the IntPtr (given that it is a PIDL) and call the P/Invoke method SHGetPathFromIDListW(), which will put the string representation of the PIDL in our StringBuilder. 然后我们获取IntPtr (假设它是一个PIDL)并调用P / Invoke方法SHGetPathFromIDListW(),它将把PIDL的字符串表示形式放在我们的StringBuilder中。

private string GetPathFromPIDL(byte[] byteCode)
    {
        //MAX_PATH = 260
        StringBuilder builder = new StringBuilder(260);

        IntPtr ptr = IntPtr.Zero;
        GCHandle h0 = GCHandle.Alloc(byteCode, GCHandleType.Pinned);
        try
        {
            ptr = h0.AddrOfPinnedObject();
        }
        finally
        {
            h0.Free();
        }

        SHGetPathFromIDListW(ptr, builder);

        return builder.ToString();
    }

Prototype for SHGetPathFromIDListW() : SHGetPathFromIDListW()原型:

[DllImport("shell32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SHGetPathFromIDListW(IntPtr pidl, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder pszPath);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误在C#调用存储过程中从'System.String'到'System.Byte []'的无效转换 - Error Invalid cast from 'System.String' to 'System.Byte[]' in C# calling stored procedure 字符串值 system.byte[] 同时将 Base64 值转换为 C# 中的字符串 - String value system.byte[] while coverting Base64 value to string in c# 下载 txt 文件显示 System.Byte[] 以及如何将其转换为字符串? C# - Downloading a txt file shows System.Byte[] and how to convert it to a string? c# 如何将 System.Byte[] 转换为要显示到 Word C# 中的图像 - How to convert System.Byte[] into an Image to be Displayed into Word C# 从c#查询MySQL返回System.Byte [] - Query to MySQL from c# returns System.Byte[] .NET C#mysql SUBSTRING给了我System.Byte []? - .NET C# mysql SUBSTRING gives me System.Byte[]? 在C#中读取pdf文件时出现System.byte错误 - System.byte error while reading a pdf file in C# 如何在 C# 中将 struct System.Byte byte[] 转换为 System.IO.Stream 对象? - How do I convert struct System.Byte byte[] to a System.IO.Stream object in C#? 将System.Byte []转换为字符串或图像格式 - convert System.Byte[] to string or image format 从System.Byte []转换为String失败 - Convert from System.Byte[] to String fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM