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

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

问题描述

我想从 IE (.NET 4) 获取当前 URL.为此,我添加了对 Microsoft Interner Controls 的引用并添加了代码(来自 http://omegacoder.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.

如何解决?

推荐答案

第二个错误导致第一个错误.打开项目的 References 节点,选择 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 interface 类型,而不能嵌入 .因此,请避免在您的代码中使用合成的 XxxxClass 包装器.让它看起来像这样:

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# 语言的接口类型上使用 new 运算符.但实际上支持 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天全站免登陆