Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 776
Tony Avatar asked Nov 13 '10 18:11

Tony


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


1 Answers

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.

like image 148
Hans Passant Avatar answered Nov 11 '22 05:11

Hans Passant