时间:2019-03-17 标签:c#RegistrySystem.Byte[]tostring
我目前正在编写一个程序,该程序将读取部分 Windows 系统注册表,但是这些键的某些值属于 System.Byte[] 类型,当我尝试解码这些值时,我可以生成一个包含一些可读字符的字符串,该字符串使但大多数字符串都是乱码。我尝试了几种编码类型,但似乎都没有产生正确的结果。我只是想知道是否有任何已知的东西可以解决这个问题。这是我的代码
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)
}
}
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. 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)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正确的解码和解释因键而异。对于二进制值,没有强制格式,应用程序可以自由存储它们想要的任何字节。你必须知道你所读到的内容才能解释它。如果关键内容已记录,那么您可以应用文档规范来解码内容。如果没有记录,那么你就没有必要阅读它。
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.
也许这不再是一个问题,但为了帮助可能遇到此问题的其他人,请考虑以下内容:
PIDL(或指向项目标识符列表的指针)仅存在于 Windows NT 6.x(Vista、Win7)中,并且实际上从桌面而不是 C:\... (我相信这是由于引入了“库”而完成的)。因此,您需要进行特殊处理才能从 PIDL 中提取文件路径。
以下方法 GetPathFromPIDL() 将获取注册表项的
(byte[]) byteCode
并将其转换为特定于平台的IntPtr
。然后,我们获取 IntPtr(假设它是 PIDL)并调用 P/Invoke 方法 SHGetPathFromIDListW(),这会将 PIDL 的字符串表示形式放入 StringBuilder 中。SHGetPathFromIDListW()
的原型: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"). Thus, you need special handling to extract a file path from a PIDL.
The following method, GetPathFromPIDL(), will take a registry key's
(byte[]) byteCode
and covert it to a platform specificIntPtr
. Then we take theIntPtr
(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.Prototype for
SHGetPathFromIDListW()
:不保证存储在注册表中的任何值的字节都一定是编码字符串。如果它们是字符串,那么注册表值的类型就是字符串。
如果它是一个字节值,那么它通常意味着它是应用程序自定义的某种数据结构的编码,因此您必须弄清楚拥有您正在读取的密钥的应用程序的格式是什么。
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.
从注册表项的名称来看,我猜测这些 blob 根本不是编码的文件名字符串,而是 PIDL(或更确切地说 ITEMIDLIST)。 PIDL 是表示 shell 命名空间中的实体的 shell 结构,它不必是文件(例如控制面板)。
如果是这种情况,您可能需要使用 SHGetPathFromIDList API 将 PIDL 转换为文件系统路径:将 byte[] 数组的地址作为 pidl 参数传入。
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). A PIDL is a shell structure representing an entity in the shell namespace, which need not be a file (e.g. Control Panel).
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.