通过注册表添加网址到IE兼容视图

扯0 2014-04-20 09:38:07
已找到兼容视图对应的注册表项HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\BrowserEmulation\ClearableListData,但 UserFilter值是二进制类型 ,没有找到该值的规律 !从而实现通过注册表添加网址到IE兼容视图。

谁有过这方面研究,求教!!!

以下为测试所用:
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\BrowserEmulation\ClearableListData" /v UserFilter /t REG_BINARY /d " 411f00005308adba010000003a00000001000000010000000c00000056f2612b265acf01010000000e003200310038002e003200340037002e003200330037002e0034003000 "

现在想实现的是输入网址,转成此类型的值!
...全文
2666 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
扯0 2014-04-25
  • 打赏
  • 举报
回复
谢谢斑竹的回答,我之前已经通过你给的案例实现了,对于你这次的完整代码正好也学习学习,多谢!!!
孟子E章 2014-04-20
  • 打赏
  • 举报
回复
这个页面的下面的代码可以实现 http://stackoverflow.com/questions/16722454/editing-binary-registry-key 前提是里面已经有值,你可以再改改 测试例子,IE8+Windows 2003测试通过
private const string CLEARABLE_LIST_DATA = @"Software\Microsoft\Internet Explorer\BrowserEmulation\ClearableListData";
    private const string USERFILTER = "UserFilter";

    private static string[] GetDomains()
    {
      string[] domains;

      using (Microsoft.Win32.RegistryKey regkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(CLEARABLE_LIST_DATA))
      {
        byte[] filter = regkey.GetValue(USERFILTER) as byte[];
        domains = GetDomains(filter);
      }

      return domains;
    }

    /* IT'S DANGER!! */
    // You shouldn't call until it becomes completely obvious that UNKNOWN parameter is meaning.
    private static void AddUserFilter(string domain)
    {
      string[] domains = GetDomains();
      foreach (string item in domains)
      {
        if (item == domain)
        {
          return;
        }
      }

      using (Microsoft.Win32.RegistryKey regkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(CLEARABLE_LIST_DATA))
      {
        byte[] filter = regkey.GetValue(USERFILTER) as byte[];
        if (filter == null) filter = new byte[0];
        byte[] newReg = GetAddedValue(domain, filter);

        regkey.SetValue(USERFILTER, newReg, Microsoft.Win32.RegistryValueKind.Binary);
      }
    }

    private static void RemoveUserFilter(string domain)
    {
      using (Microsoft.Win32.RegistryKey regkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(CLEARABLE_LIST_DATA))
      {
        byte[] filter = regkey.GetValue(USERFILTER) as byte[];
        byte[] newReg = GetRemovedValue(domain, filter);

        if (GetDomains(newReg).Length == 0)
          regkey.DeleteValue(USERFILTER);
        else
          regkey.SetValue(USERFILTER, newReg, Microsoft.Win32.RegistryValueKind.Binary);
      }
    }

    private static byte[] GetFilter()
    {
      byte[] filter;
      using (Microsoft.Win32.RegistryKey regkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(CLEARABLE_LIST_DATA))
      {
        filter = regkey.GetValue(USERFILTER) as byte[];
      }
      return filter;
    }

    private static string[] GetDomains(byte[] filter)
    {
      List<string> domains = new List<string>();

      int length;
      int offset_filter = 24;
      int totalSize = filter.Length;

      while (offset_filter < totalSize)
      {
        length = BitConverter.ToUInt16(filter, offset_filter + 16);
        domains.Add(System.Text.Encoding.Unicode.GetString(filter, 16 + 2 + offset_filter, length * 2));
        offset_filter += 16 + 2 + length * 2;
      }

      return domains.ToArray();
    }

    private static byte[] GetAddedValue(string domain, byte[] filter)
    {
      byte[] SEPARATOR = new byte[] { 0x0C, 0x00, 0x00, 0x00 };
      byte[] CONSTANT = new byte[] { 0x01, 0x00, 0x00, 0x00 };
      byte[] UNKNOWN = BitConverter.GetBytes(DateTime.Now.ToBinary());

      List<byte> newReg = new List<byte>();
      byte[] binDomain = System.Text.Encoding.Unicode.GetBytes(domain);

      newReg.AddRange(filter);
      newReg.AddRange(SEPARATOR);
      /************************************************************************************************/
      newReg.AddRange(UNKNOWN); // IT'S IRRESPONSIBLE!!  Setting 0x00 is preferable to adding DateTime
      /************************************************************************************************/
      newReg.AddRange(CONSTANT);
      newReg.AddRange(BitConverter.GetBytes((UInt16)domain.Length));
      newReg.AddRange(binDomain);

      byte[] newSize = BitConverter.GetBytes((UInt16)(newReg.Count - 12));
      newReg[12] = newSize[0];
      newReg[13] = newSize[1];

      string[] domains = GetDomains();
      byte[] newCount = BitConverter.GetBytes((UInt16)(domains.Length + 1));
      newReg[8] = newCount[0];
      newReg[9] = newCount[1];

      newReg[20] = newCount[0];
      newReg[21] = newCount[1];

      return newReg.ToArray();
    }

    private static byte[] GetRemovedValue(string domain, byte[] filter)
    {
      byte[] newReg;
      int length;
      int offset_filter = 24;
      int offset_newReg = 0;
      int totalSize = filter.Length;

      newReg = new byte[totalSize];
      Array.Copy(filter, 0, newReg, 0, offset_filter);
      offset_newReg += offset_filter;

      while (offset_filter < totalSize)
      {
        length = BitConverter.ToUInt16(filter, offset_filter + 16);
        if (domain != System.Text.Encoding.Unicode.GetString(filter, offset_filter + 16 + 2, length * 2))
        {
          Array.Copy(filter, offset_filter, newReg, offset_newReg, 16 + 2 + length * 2);
          offset_newReg += 16 + 2 + length * 2;
        }
        offset_filter += 16 + 2 + length * 2;
      }
      Array.Resize(ref newReg, offset_newReg);
      byte[] newSize = BitConverter.GetBytes((UInt16)(offset_newReg - 12));
      newReg[12] = newSize[0];
      newReg[13] = newSize[1];

      return newReg;
    }
    private void Form1_Load(object sender, EventArgs e)
    {
      AddUserFilter("meng.com");
    }
扯0 2014-04-20
  • 打赏
  • 举报
回复
谢谢你的回答,不过不能被正常转换!
孟子E章 2014-04-20
  • 打赏
  • 举报
回复
试试
Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\BrowserEmulation\ClearableListData", "UserFilter", System.Text.Encoding.ASCII.GetBytes("https://www.google.com.hk"), RegistryValueKind.Binary);
孟子E章 2014-04-20
  • 打赏
  • 举报
回复
完整代码发布在 https://code.csdn.net/snippets/302829 请查收

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧