C#如何从IE浏览器获取当前URL? [英] C# How to get current URL from the IE?

查看:753
本文介绍了C#如何从IE浏览器获取当前URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从IE(.NET 4)当前的URL。要做到这一点,我加入微软Interner控制参考,并添加了code(从的http://欧米茄coder.com / p = 63

I want to get the current URL from the IE (.NET 4) . To do so, I added a reference to Microsoft Interner Controls and added the code (from http://omegacoder.com/?p=63)

foreach (InternetExplorer ie in new ShellWindowsClass())
{
   textBox1.Text = ie.LocationURL.ToString();
}

但我得到2个错误:

but I get 2 errors:

1] The type 'SHDocVw.ShellWindowsClass' has no constructors defined

2] Interop type 'SHDocVw.ShellWindowsClass' cannot be embedded.
   Use the applicable interface instead.

如何解决呢?

推荐答案

第二错误导致的第一个。打开项目的参考节点,选择SHDOCVW。在属性窗口中更改嵌入互操作类型设置为false。你将不得不部署Interop.SHDocVw.dll组装,你会与程序一起找到构建输出目录。

The 2nd error causes the first one. Open the project's References node, select SHDocVw. In the Properties window, change "Embed Interop Types" to false. You will have to deploy the Interop.SHDocVw.dll assembly you'll find the build output directory along with your program.

编辑:研究这个错误之后,我发现了一个更好的办法来做到这一点。问题是,只有COM的接口的类型可以嵌入,不是的的。所以要避免使用合成的 XxxxClass 的在code包装。使它看起来像这个:

after researching this error, I found a better way to do this. The issue is that only COM interface types can be embedded, not classes. So avoid using the synthetic XxxxClass wrappers in your code. Make it look like this instead:

        foreach (InternetExplorer ie in new ShellWindows()) {
            //...
        }

这看起来很奇怪,你不能正常使用的新的的运营商在C#语言的接口类型。但实际上是支持COM接口。

Which looks strange, you cannot normally use the new operator on an interface type in the C# language. But is actually supported for COM interfaces.

这篇关于C#如何从IE浏览器获取当前URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆