C# 实现向浏览器的兼容性视图列表中添加、删除网站和检查网站是否在兼容性网站列表中
今天回答论坛上的一个问题,搜索了一下网上,并没有找到一个完整的例子,下面根据网上的一些资料,经过转换、完善成一个完整的例子。下面的例子可以实现添加、删除、检测网站是否在兼容性网站列表中的功能。
以下代码在IE8、Windows2003环境下测试通过。测试时要重新打开浏览器进行验证。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;namespace WindowsFormsApplication4
{public partial class Form1 : Form{public Form1(){InitializeComponent();}/** * 添加、删除兼容性网站列表的代码。* * 孟宪会@2014-04-20* *///兼容性列表在注册表中的位置,注意:不同位的操作系统可能不同,请注意测试。private const string CLEARABLE_LIST_DATA = @"Software\Microsoft\Internet Explorer\BrowserEmulation\ClearableListData";private const string USERFILTER = "UserFilter";private byte[] header = new byte[] { 0x41, 0x1F, 0x00, 0x00, 0x53, 0x08, 0xAD, 0xBA };private byte[] delim_a = new byte[] { 0x01, 0x00, 0x00, 0x00 };private byte[] delim_b = new byte[] { 0x0C, 0x00, 0x00, 0x00 };private byte[] checksum = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF };private byte[] filler = BitConverter.GetBytes(DateTime.Now.ToBinary());//new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };private byte[] regbinary = new byte[] { };/// <summary>/// 得到已经存在的所有兼容网站列表,如果没有,则返回空数组。/// </summary>/// <returns></returns>private string[] GetDomains(){string[] domains = { };using (RegistryKey regkey = Registry.CurrentUser.OpenSubKey(CLEARABLE_LIST_DATA)){//可能不存在此key.Object filterData = regkey.GetValue(USERFILTER);if (filterData != null){byte[] filter = filterData as byte[];domains = GetDomains(filter);}}return domains;}/// <summary>/// 从byte数组中分析所有网站名称/// </summary>/// <param name="filter"></param>/// <returns></returns>private 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();}/// <summary>/// 从兼容性列表中删除一个网站。/// </summary>/// <param name="domain">要删除网站</param>private void RemoveUserFilter(string domain){String[] domains = GetDomains();if (!domains.Contains(domain)) {return;}using (RegistryKey regkey = Registry.CurrentUser.OpenSubKey(CLEARABLE_LIST_DATA,true)){object oldData = regkey.GetValue(USERFILTER);if (oldData != null){byte[] filter = oldData as byte[];byte[] newReg = GetRemovedValue(domain, filter);if (GetDomains(newReg).Length == 0)regkey.DeleteValue(USERFILTER);elseregkey.SetValue(USERFILTER, newReg, RegistryValueKind.Binary);}}}/// <summary>/// 得到一个网站的存储的数据/// </summary>/// <param name="domain"></param>/// <param name="filter"></param>/// <returns></returns>private 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;}/// <summary>/// 向兼容性列表中添加一个网站/// </summary>/// <param name="domain"></param>private void AddNewSiteToCompatibilityViewList(String domain){String[] domains = GetDomains();if (domains.Length > 0){if (domains.Contains(domain)){return;}else{domains = domains.Concat(new String[] { domain }).ToArray();}}else{domains = domains.Concat(new String[] { domain }).ToArray();}int count = domains.Length;byte[] entries = new byte[0];foreach (String d in domains){entries = this.Combine(entries, this.GetDomainEntry(d));}regbinary = header;regbinary = this.Combine(regbinary, BitConverter.GetBytes(count));regbinary = this.Combine(regbinary, checksum);regbinary = this.Combine(regbinary, delim_a);regbinary = this.Combine(regbinary, BitConverter.GetBytes(count));regbinary = this.Combine(regbinary, entries);Registry.CurrentUser.OpenSubKey(CLEARABLE_LIST_DATA,true).SetValue(USERFILTER, regbinary, RegistryValueKind.Binary);}/// <summary>/// 得到一个网站在兼容性列表中的数据,跟GetRemovedValue类似/// </summary>/// <param name="domain"></param>/// <returns></returns>private byte[] GetDomainEntry(String domain){byte[] tmpbinary = new byte[0];byte[] length = BitConverter.GetBytes((UInt16)domain.Length);byte[] data = System.Text.Encoding.Unicode.GetBytes(domain);tmpbinary = Combine(tmpbinary, delim_b);tmpbinary = Combine(tmpbinary, filler);tmpbinary = Combine(tmpbinary, delim_a);tmpbinary = Combine(tmpbinary, length);tmpbinary = Combine(tmpbinary, data);return tmpbinary;}//把两个byte[]数组合并在一起private byte[] Combine(byte[] a, byte[] b){byte[] c = new byte[a.Length + b.Length];System.Buffer.BlockCopy(a, 0, c, 0, a.Length);System.Buffer.BlockCopy(b, 0, c, a.Length, b.Length);return c;}private void Form1_Load(object sender, EventArgs e){//添加10个for (int i = 0; i < 10; i++){AddNewSiteToCompatibilityViewList("meng" + i + ".com");}//删除2个for (int i = 3; i < 5; i++){this.RemoveUserFilter("meng" + i + ".com");}}}
}
网上的3篇资料与本文关系较大。
http://www3.xuebuyuan.com/1567759.html
http://jeffgraves.me/2014/02/19/modifying-ie-compatibility-view-settings-with-powershell/
http://stackoverflow.com/questions/16722454/editing-binary-registry-key
为了确保原文的丢失能找到内容,先把有用的PowerShell脚本粘贴过来。
$key = "HKCU:\Software\Microsoft\Internet Explorer\BrowserEmulation\ClearableListData"
$item = "UserFilter". .\Get-IPrange.ps1
$cidr = "^((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)/(3[0-2]|[1-2]?[0-9])$"[byte[]] $regbinary = @()#This seems constant
[byte[]] $header = 0x41,0x1F,0x00,0x00,0x53,0x08,0xAD,0xBA#This appears to be some internal value delimeter
[byte[]] $delim_a = 0x01,0x00,0x00,0x00#This appears to separate entries
[byte[]] $delim_b = 0x0C,0x00,0x00,0x00#This is some sort of checksum, but this value seems to work
[byte[]] $checksum = 0xFF,0xFF,0xFF,0xFF#This could be some sort of timestamp for each entry ending with 0x01, but setting to this value seems to work
[byte[]] $filler = 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01#Examples: mydomain.com, 192.168.1.0/24
$domains = @("google.com","192.168.1.0/24")function Get-DomainEntry($domain) {
[byte[]] $tmpbinary = @()[byte[]] $length = [BitConverter]::GetBytes([int16]$domain.Length)
[byte[]] $data = [System.Text.Encoding]::Unicode.GetBytes($domain)$tmpbinary += $delim_b
$tmpbinary += $filler
$tmpbinary += $delim_a
$tmpbinary += $length
$tmpbinary += $datareturn $tmpbinary
}if($domains.Length -gt 0) {
[int32] $count = $domains.Length[byte[]] $entries = @()foreach($domain in $domains) {
if($domain -match $cidr) {
$network = $domain.Split("/")[0]
$subnet = $domain.Split("/")[1]
$ips = Get-IPrange -ip $network -cidr $subnet
$ips | %{$entries += Get-DomainEntry $_}
$count = $count - 1 + $ips.Length
}
else {
$entries += Get-DomainEntry $domain
}
}$regbinary = $header
$regbinary += [byte[]] [BitConverter]::GetBytes($count)
$regbinary += $checksum
$regbinary += $delim_a
$regbinary += [byte[]] [BitConverter]::GetBytes($count)
$regbinary += $entries
}Set-ItemProperty -Path $key -Name $item -Value $regbinary
IE11下的PowerShell
$key = "HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft"if(-Not (Test-Path "$key\Internet Explorer")) {
New-Item -Path $key -Name "Internet Explorer" | Out-Null
}if(-Not (Test-Path "$key\Internet Explorer\BrowserEmulation")) {
New-Item -Path "$key\Internet Explorer" -Name "BrowserEmulation" | Out-Null
}if(-Not (Test-Path "$key\Internet Explorer\BrowserEmulation\PolicyList")) {
New-Item -Path "$key\Internet Explorer\BrowserEmulation" -Name "PolicyList" | Out-Null
}. .\Get-IPrange.ps1
$cidr = "^((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)/(3[0-2]|[1-2]?[0-9])$"#Examples: mydomain.com, 192.168.1.0/24
$domains = @("google.com","192.168.1.0/24")$regkey = "$key\Internet Explorer\BrowserEmulation\PolicyList"foreach($domain in $domains) {
if($domain -match $cidr) {
$network = $domain.Split("/")[0]
$subnet = $domain.Split("/")[1]
$ips = Get-IPrange -ip $network -cidr $subnet
$ips | %{$val = New-ItemProperty -Path $regkey -Name $_ -Value $_ -PropertyType String | Out-Null}
$count = $count - 1 + $ips.Length
}
else {
New-ItemProperty -Path $regkey -Name $domain -Value $domain -PropertyType String | Out-Null
}
}
C# 实现向浏览器的兼容性视图列表中添加、删除网站和检查网站是否在兼容性网站列表中相关推荐
- Vue之通过连接数据库的接口获取列表实现添加删除功能
把最近学习vue的一些知识点记录下来,今天记录一下Vue通过vue-resource连接数据库接口渲染列表和添加删除功能 首先我们得引入vue的版本文件和vue-resource.js,注意:vue- ...
- 在浏览器中添加删除页面元素
在浏览器中添加删除页面元素 一.添加元素 确定添加元素位置,并找到容器元素. 新建一个填写内容步骤,添加填写项目,获取容器元素,填写属性为添加子元素addchild,填写内容为新元素的html代码. ...
- 在word文档中添加目录(包括项目符号、编号、多级列表使用方法)
1.选择自己喜欢的多级列表 2.自己选择目前需要的编号 如果你在某一行文字前也想要添加编号,那么只需要点一次多级列表,它出现1.以后自己按tab键选择需要的编号级数: 3.当你每一级都弄好以后(如上图 ...
- linux列表中添加多个元素,这12种方法轻松合并Python中的列表
导读 我喜欢12这个数字,因为有太多的事情与12有关.一年有12个月;古代用12个时辰(地支)表示一天的时间.用一纪表示12年;希腊有12主神;黄道有12宫;撒迦利亚.西琴先生的<地球编年史&g ...
- Mozilla计划向Firefox浏览器中添加违规警告
Mozilla 计划向 Firefox 浏览器中添加警告通知,当用户访问过得网站被黑客入侵时. 黑客入侵网络服务和网站是互联网上常见的事情,虽然黑客可能有不同的目的,但他们都想盗取网站的用户数据库. ...
- java swing 列表框_Java开发笔记(一百三十一)Swing的列表框
前面介绍了选择框的用法,当时为了方便用户勾勾点点,无论是复选框还是单选按钮,统统把所有选项都摆在界面上.倘若只有两三个选项还好办,要是选项数量变多比如超过五个,这么多的选择框一齐在界面罗列,不光程序员 ...
- html如何设置ie6兼容性视图,IE6浏览器兼容性视图设置在哪里
ie6浏览器算是旧版本了,如果你想要设置兼容性视图,该怎么设置呢?下面由学习啦小编为大家整理了IE6浏览器的兼容性视图设置在哪里的方法,希望对大家有帮助! IE6浏览器兼容性视图设置在哪里 IE6兼容 ...
- html中添加ie兼容性视图,IE11浏览器添加到兼容性视图中的网站无法保存怎么办?...
IE11浏览器添加到兼容性视图中的网站无法保存怎么办?有用户在使用IE11浏览器时遇到了瓶颈,就是在兼容性列表中保存网站了,当重启浏览器之后,添加保存的网址和设置都没有了,为什么兼容性视图网站列表会无 ...
- html设置ie9兼容性视图,ie9浏览器设置兼容性视图在哪里设置
当您在使用IE9打开为早期IE版本所设计的网页时,可能会无法正常浏览.此时您可以尝试一下IE的兼容性视图功能来解决这个问题.下面由学习啦小编为你整理了ie9浏览器设置兼容性视图在哪里设置的相关方法,希 ...
最新文章
- html编辑器渗透,渗透笔记40、web编辑器漏洞手册.pdf
- 优化杭州某著名电子商务网站高并发千万级大型数据库经验之- 磁盘I/O性能
- 函数动态参数实现format
- python 购物车程序_购物车程序python
- java元数据是什么_Java和大数据是什么关系 杭州哪个就业前景更好
- 俄罗斯方块是java_俄罗斯方块java
- 开发信模板之失联跟进样品篇
- 《大型网站技术架构》阅读笔记
- html弹窗可以复制,简单漂亮的js弹窗可自由拖拽且兼容大部分浏览器
- 基于SSM实现微博系统
- Python人眼视线追踪系统
- 教师进修学校计算机教案,信息化教学教案
- java 多线程详细(转载)
- 2020美赛F题翻译
- SHIMANO各个等级配件的区别
- 共读ulord项目分析(一)
- 踩坑!穿山甲广告Android SDK接入
- R语言logistic回归的细节解读
- 状态栏QStatusBar
- 智能制造数字化工厂的关键技术特点