+ -
当前位置:首页 → 问答吧 → C#怎么实现从浏览器获取当前访问网页的URL

C#怎么实现从浏览器获取当前访问网页的URL

时间:2011-12-11

来源:互联网

我用C#winform写了一个小程序,想从浏览器上获得当前访问网页的URL。我的方法是:
[code=C#][/code]ShellWindows shellwindows = new ShellWindows();
   
  foreach (InternetExplorer ie in shellwindows)
  {
  string filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
  if (filename.Equals("iexplore"))
  {
  if (txt_ReceiveUrl .Text !=ie.LocationURL .ToString ())
  {  
  txt_ReceiveUrl.Text = ie.LocationURL.ToString();
  txt_pageTitle.Text = ie.FullName.ToString();
   
  }
  }
  }

这种方只能从系统自带的IE浏览器上获得URL,但是,无法从其他浏览器上获得URL,我试了 搜狗浏览器、遨游浏览器,都无法从上面获得URL。请问各位大侠,我怎么才能其它的浏览器上获得当前访问网页的URL呢?

作者: monkin2011   发布时间: 2011-12-11

using System;
 using System.Windows.Forms;
 using SHDocVw;
 namespace WindowsApplication35
  ... {
  public partial class Form1 : Form
  ... {
  public Form1()
  ... {
  InitializeComponent();
  } 
  private void Form1_Load( object sender, EventArgs e)
  ... {
  ShellWindowsClass shellWindows = new ShellWindowsClass();
  foreach (InternetExplorer ie in shellWindows)
  ... {
  string filename = System.IO.Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
 
  if (filename.Equals( " iexplore " ))
  ... {
  Console.WriteLine(ie.LocationURL);
  } 
  } 
  } 
 
   
  } 
 } 


webBrowser1.Document.window.loaction.href;
利用的是webBrowser对象的属性,索引到页面的window对象而读取的当前页面的链接地址,进行读取.
或许这并不是个好方法。
具体的内容,可以查看这个对象的属性方法.

webBrowser1.DocumentText;
是整个页面的源文件.

你可以使用浏览器的api来获取

作者: hefeng_aspnet   发布时间: 2011-12-11

相关阅读 更多