简体   繁体   中英

C# How to get current URL from the IE?

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();
}

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.

How to solve that ?

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.

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 . So avoid using the synthetic XxxxClass wrappers in your code. 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. But is actually supported for COM interfaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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