简体   繁体   English

C#如何从IE获取当前URL?

[英]C# How to get current URL from the IE?

I want to get the current URL from the IE (.NET 4) . 我想从IE(.NET 4)获取当前的URL。 To do so, I added a reference to Microsoft Interner Controls and added the code (from http://omegacoder.com/?p=63 ) 为此,我添加了对Microsoft Interner Controls的引用并添加了代码(来自http://omegacoder.com/?p=63

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

but I get 2 errors: 但我得到2个错误:

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

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

How to solve that ? 怎么解决?

The 2nd error causes the first one. 第二个错误导致第一个错误。 Open the project's References node, select SHDocVw. 打开项目的References节点,选择SHDocVw。 In the Properties window, change "Embed Interop Types" to false. 在“属性”窗口中,将“嵌入互操作类型”更改为false。 You will have to deploy the Interop.SHDocVw.dll assembly you'll find the build output directory along with your program. 您将必须部署Interop.SHDocVw.dll程序集,您将找到构建输出目录以及您的程序。

EDIT: 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 . 问题是只能嵌入COM 接口类型,而不能嵌入 So avoid using the synthetic XxxxClass wrappers in your code. 因此,请避免在代码中使用合成的XxxxClass包装器。 Make it look like this instead: 让它看起来像这样:

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

Which looks strange, you cannot normally use the new operator on an interface type in the C# language. 这看起来很奇怪,你通常不能在C#语言的接口类型上使用new运算符。 But is actually supported for COM interfaces. 但实际上支持COM接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM