C#,如何获取IE浏览器打开的名单(如图的所有姓名,身份证明号码,车型),并导入到dataGridView
如果你想从 Internet Explorer 浏览器获取已打开的标签并导入到 DataGridView 控件,可以使用 C# 语言编写代码。下面是实现这个功能的代码示例:
using System;
using System.Windows.Forms;
using SHDocVw;
namespace GetIEHistory
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
InternetExplorer ie = new InternetExplorerClass();
var history = ie.History as IEnumVARIANT;
int cnt = ie.History.Count;
for (int i = 0; i < cnt; i++)
{
history.Next(1, ref varUrl, out l);
string url = varUrl.ToString();
dataGridView1.Rows.Add(new object[] { url });
}
}
}
}
作者说的打开浏览器的名单具体指的是什么,描述详细一些
可以使用Windows自动化技术,如Windows UI Automation(UIA),来获取Internet Explorer浏览器中打开的信息。
具体来说,需要使用UIA自动化库,如Windows Automation API或UIAutomationClient,找到Internet Explorer窗口,然后遍历其子元素(如HTML元素),以获取所需的信息。可以使用元素的属性(例如标签名,类名等)来确定要提取的元素。
下面是一个示例代码,演示了如何使用UIAutomationClient库获取IE浏览器的标题:
using System;
using System.Windows.Automation;
namespace IE_Automation_Sample
{
class Program
{
static void Main(string[] args)
{
AutomationElement rootElement = AutomationElement.RootElement;
AutomationElement ieWindow = FindIEWindow(rootElement);
if (ieWindow != null)
{
Console.WriteLine("IE Window Title: " + ieWindow.Current.Name);
}
else
{
Console.WriteLine("IE Window not found.");
}
}
private static AutomationElement FindIEWindow(AutomationElement rootElement)
{
AutomationElementCollection windowElements = rootElement.FindAll(TreeScope.Children,
new PropertyCondition(AutomationElement.ClassNameProperty, "IEFrame"));
foreach (AutomationElement windowElement in windowElements)
{
if (windowElement.Current.Name.Contains("Internet Explorer"))
{
return windowElement;
}
}
return null;
}
}
}
一旦获取了所需的信息,可以将其存储在DataGridView控件中,以显示在窗体中。
以下是一个简单的示例,说明如何将数据导入到DataGridView:
using System;
using System.Data;
using System.Windows.Forms;
namespace DataGridViewSample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// 创建一个DataTable
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Id", typeof(string));
dt.Columns.Add("Vehicle", typeof(string));
// 向DataTable中添加数据
dt.Rows.Add("John Doe", "123456", "Car");
dt.Rows.Add("Jane Doe", "234567", "Bike");
dt.Rows.Add("Jim Smith", "345678", "Truck");
// 将DataTable绑定到DataGridView
dataGridView1.DataSource = dt;
}
}
}
想从IE浏览器中获取信息并导入到DataGridView中,需要执行以下步骤:
使用WebBrowser控件加载页面。
使用HTML Agility Pack(或其他HTML解析库)解析HTML文档,获取所需的信息。
将信息存储在数据结构中,例如List或DataTable。
将数据结构绑定到DataGridView。
示例代码:
```private void GetDataFromBrowser()
{
// 创建WebBrowser对象并加载页面
WebBrowser webBrowser = new WebBrowser();
webBrowser.Navigate("https://www.example.com");
// 等待页面加载完成
while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
// 使用HTML Agility Pack解析HTML文档
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(webBrowser.DocumentText);
// 获取所需信息
List<string> names = new List<string>();
List<string> idNumbers = new List<string>();
List<string> vehicleTypes = new List<string>();
foreach (HtmlNode node in htmlDoc.DocumentNode.SelectNodes("//table/tr"))
{
names.Add(node.SelectSingleNode(".//td[1]").InnerText);
idNumbers.Add(node.SelectSingleNode(".//td[2]").InnerText);
vehicleTypes.Add(node.SelectSingleNode(".//td[3]").InnerText);
}
// 将信息存储在DataTable中
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Name", typeof(string));
dataTable.Columns.Add("ID Number", typeof(string));
dataTable.Columns.Add("Vehicle Type", typeof(string));
for (int i = 0; i < names.Count; i++)
{
dataTable.Rows.Add(names[i], idNumbers[i], vehicleTypes[i]);
}
// 绑定DataGridView
dataGridView1.DataSource = dataTable;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:下面是如何在 C# 中通过代码获取 Internet Explorer 浏览器打开的页面列表,并导入到 DataGridView 的代码示例:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using mshtml;
namespace GetIEUrl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// 获取 Internet Explorer 实例
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
// 获取页面标题和 URL
string title = ie.LocationName;
string url = ie.LocationURL;
// 将数据添加到 DataGridView
dataGridView1.Rows.Add(title, url);
}
}
}
}
这段代码使用了 SHDocVw.ShellWindows 类获取 Internet Explorer 实例,并遍历每个实例,从中获取页面的标题和 URL,最后将数据添加到 DataGridView 中。