454

Is there any relatively easy way to insert a modern browser into a .NET application?

As far as I understand, the WebBrowser control is a wrapper for IE, which wouldn't be a problem except that it looks like it is a very old version of IE, with all that entails in terms of CSS screw-ups, potential security risks (if the rendering engine wasn't patched, can I really expect the zillion buffer overflow problems to be fixed?), and other issues.

I am using Visual Studio C# (express edition - does it make any difference here?)

I would like to integrate a good web browser in my applications. In some, I just use it to handle the user registration process, interface with some of my website's features and other things of that order, but I have another application in mind that will require more err... control.

I need:

  • A browser that can integrate inside a window of my application (not a separate window)
  • A good support for CSS, js and other web technologies, on par with any modern browser
  • Basic browser functions like "navigate", "back", "reload"...
  • Liberal access to the page code and output.

I was thinking about Chrome, since it comes under the BSD license, but I would be just as happy with a recent version of IE.

As much as possible, I would like to keep things simple. The best would be if one could patch the existing WebBrowser control, which does already about 70% of what I need, but I don't think that's possible.

I have found an activeX control for Mozilla (http://www.iol.ie/~locka/mozilla/control.htm) but it looks like it's an old version, so it's not necessarily an improvement.

I am open to suggestions

Ashkan Mobayen Khiabani
  • 30,915
  • 26
  • 90
  • 147
Sylverdrag
  • 8,560
  • 5
  • 32
  • 49
  • 19
    Why do you say that the WebBrowser control uses an old version of IE? It uses the version installed on the user's system, although the IE8 WebBrowser appears to default to IE7 rendering: http://blogs.msdn.com/ie/archive/2009/03/10/more-ie8-extensibility-improvements.aspx – Tim Robinson Apr 26 '09 at 09:25
  • 18
    Because it doesn't look that way on my machine. I have IE8 installed but the control shows display issues that I haven't see since IE 5. A very simple login form, 2 fields with a touch of CSS has a garbled display, and some javascript display doesn't work, whereas it displays fine in IE8, Firefox, Chrome, Opera ... so I assumed the rendering engine was an old one. I could be completely wrong about that and perhaps the problem is in fact different from what I thought. – Sylverdrag Apr 26 '09 at 09:38
  • 3
    @Sylverdrag: You are wrong. It uses the latest IE on your system. However, I read somewhere that the WebBrowser control has a stronger backwards-compatibility issue than the standalone browser. IE8 can have, for instance, an icon to click to turn on IE7 mode. As part of your program, this is not possible, so the control defaults to the earlier mode for compatability. OTOH, I haven't read how to set it to use "IE8 mode". – John Saunders Apr 27 '09 at 01:35
  • @John: This is strange because the page DOES display properly in IE7 mode as well as in IE8 mode in my normal browser. (The page is http://your-translations.com/newDesign, click on the login button to see the forms that gets messed up. As you can see, it's pretty simple.) – Sylverdrag Apr 27 '09 at 04:06
  • 56
    Actually, John sanders, you are wrong. It uses ie4 and you need to change a registry value to tell it to use current. Look up "feature mode emulation" and you'll get ur answers, changed recently, used to include keyword native but they changed it, google the keywords I mentioned with "webbrowser control" and u will find msdn article. – Erx_VB.NExT.Coder Sep 30 '10 at 17:13
  • @Erx_VB.NExT.Coder: Thanks for that gem. Could you give a link to the msdn article directly, and if possible explain how to force a webbrowser control to use the latest version of IE available on the machine? – Sylverdrag Oct 07 '10 at 05:30
  • 1
    @TimRobinson It does NOT use the version that is installed on the user's system. I am running IE11, when I place a WebBrowser Control onto a new Windows Form, then run it, it displays god-aweful pages. I've even tried over 20-or-so tutorials on how to force the WebControl browser to use the latest version or the latest version that is installed but _nothing works_. It never has. I don't know what tricks you're using, but there's obviously something that we're missing because I've never been able to get it to work nor has anybody I know. – jay_t55 Jun 10 '14 at 00:53
  • 9
    By default, a hosted `WebBrowser` control uses IE7 emulation, unless instructed otherwise with `FEATURE_BROWSER_EMULATION`. This is documented here: http://blogs.msdn.com/b/askie/archive/2009/03/23/understanding-compatibility-modes-in-internet-explorer-8.aspx – noseratio Jun 26 '14 at 07:27
  • 1
    That's ok it's an old question but I hope it's useful: You can set the `WebBrowser` control to use a most IE version without much pain: http://stackoverflow.com/questions/18333459/c-sharp-webbrowser-ajax-call/18333982#18333982 – Jack Jul 13 '14 at 18:25

8 Answers8

178

Checkout CefSharp .Net bindings, a project I started a while back that thankfully got picked up by the community and turned into something wonderful.

The project wraps the Chromium Embedded Framework and has been used in a number of major projects including Rdio's Windows client, Facebook Messenger for Windows and Github for Windows.

It features browser controls for WPF and Winforms and has tons of features and extension points. Being based on Chromium it's blisteringly fast too.

Grab it from NuGet: Install-Package CefSharp.Wpf or Install-Package CefSharp.WinForms

Check out examples and give your thoughts/feedback/pull-requests: https://github.com/cefsharp/CefSharp

BSD Licensed

chillitom
  • 22,164
  • 15
  • 79
  • 117
  • +1 on this answer. Now a days it is being actively developed on a released version based on Chromium 25.0.1364.152. – NucS Oct 17 '13 at 19:03
  • 2
    I switched to CEfSharp from OWS, and I can assertively say it is of very high quality and active community. Love it. https://github.com/cefsharp/CefSharp – Vin Oct 18 '13 at 18:21
  • 1
    CefSharp is great but be aware that the libraries are faily large (20+ mb last I looked) so they might not be right for a more lightweight project. – Skyl3lazer Jan 13 '15 at 19:04
  • cefsharp has one problem you cant take fullpage screenshot – skhurams Sep 19 '16 at 13:39
  • A warning when using CefSharp for browsing PDF documents in WPF in versions 53.0.0 and 51.0.0 - it doesn't. – AndyUK Nov 11 '16 at 12:05
  • Another warning WPF and offscreen rendering of videos makes it slow. – Alexandru Lache Dec 14 '16 at 20:34
  • I have a MDI Winforms app that I am migrating to a web based platform, and have added CefSharp browser controls to some forms as a temporary measure. Other forms (without the browser) have some strange repaint issues on the borders when they are moved around. Once the form with the browser control is clicked on for some action, the other forms then repaint. Wanted to mention this, thought it was strange. – Ev Conrad Feb 01 '17 at 21:01
  • From CefSharp 51, you're able to work with CefSharp using `AnyCPU` configuration. That is a great news !!!. – Rahul Apr 21 '17 at 04:12
  • 7
    This broke my project. All references broken after installing winforms nuget package. – toddmo Sep 15 '17 at 23:28
  • You will have trouble if you deploy your win form app using click-once deployment. CEFSharp dlls can't be included in the package. You will have to include the dlls manaully, using tools like Mage.exe. – Silent Sojourner Oct 30 '19 at 20:11
  • cefsharp is NOT working on VS2019 (both VB and C#)... Once I drag-and-drop the control from the toolbar it displays error message says it gana remove it from the toolbox. any idea? – Ahmed Eissa Sep 02 '20 at 13:57
  • 1
    Same for me. It forces you to change debug platform to be anything other than AnyCPU. Why do I need to change my project setting just to embed a browser? Ridiculous – Kristina Lex Oct 24 '20 at 07:03
  • 1
    @AhmedEissa I was experiencing the same issue. I resolved it by using the sample code from the quick start guide: https://github.com/cefsharp/CefSharp/wiki/Quick-Start i.e. var browser = new ChromiumWebBrowser("www.google.com"); parent.Controls.Add(browser); – drewmerk Nov 09 '20 at 21:24
84

Chrome uses (a fork of) Webkit if you didn't know, which is also used by Safari. Here's a few questions that are of the same vein:

The webkit one isn't great as the other answer states, one version no longer works (the google code one) and the Mono one is experimental. It'd be nice if someone made the effort to make a decent .NET wrapper for it but it's not something anyone seems to want to do - which is surprising given it now has support for HTML5 and so many other features that the IE(8) engine lacks.

Update (2014)

There's new dual-licensed project that allows you embed Chrome into your .NET applications called Awesomium. It comes with a .NET api but requires quite a few hacks for rendering (the examples draw the browser window to a buffer, paint the buffer as an image and refresh on a timer).

I think this is the browser used by Origin in Battlefield 3.

Update (2016)

There is now DotnetBrowser, a commercial alternative to Awesomium. It's based off Chromium.

Community
  • 1
  • 1
Chris S
  • 62,476
  • 49
  • 214
  • 238
  • Thanks. I will have a look at the Gecko wrapper. One thing though, the original answer says "All you need to do is register the Mozilla ActiveX control" and I thought that the Express editions couldn't register additional controls. Am I missing something obvious? – Sylverdrag Apr 26 '09 at 13:32
  • 4
    @Syl the express editions can register as many controls as you need. What they won't do is let you install plugins to visual studio. – Joel Coehoorn Apr 09 '10 at 14:01
  • 1
    I have had pretty bad experiences of using WebKit with C# and finally I had to settle on the default web browser component which comes with .NET – Sumit Ghosh Dec 10 '10 at 23:54
  • 1
    As of this date, the above projects are not viable drop-in replacements. Compiler errors galore + instability. Awesomium, is complete and utter trash. – Kraang Prime Apr 01 '15 at 15:10
  • "I think this is the browser used by Origin in Battlefield 3." This is incorrect. I know this because I had to force this app to use ie on windows ten because when it defaulted to edge it did not work. This was a while ago, possibly a solved issue. – Theyouthis Feb 02 '16 at 17:43
  • At the time of this answer, the first sentence was correct, but as of the release of CEF 3, Chromium and Google Chrome use the Chromium Content API and Blink layout engine which were forked off from WebKit and no longer totally interoperable. – KeithS Jul 21 '16 at 17:07
67

I've been testing alternatives to C# Web browser component for few days now and here is my list:

1. Using newer IE versions 8,9:

Web Browser component is IE7 not IE8? How to change this?

Pros:

  • Not much work required to get it running
  • some HTML5/CSS3 support if IE9, full if IE10

Cons:

  • Target machine must have target IE version installed, IE10 is still in preview on Win7

This doesn't require much work and you can get some HTML5 and CSS3 support although IE9 lacks some of best CSS3 and HTML5 features. But I'm sure you could get IE10 running same way. The problem would be that target system would have to have IE10 installed, and since is still in preview on Windows 7 I would suggest against it.

2. OpenWebKitSharp

OpenWebKitSharp is a .net wrapper for the webkit engine based on the WebKit.NET 0.5 project. WebKit is a layout engine used by Chrome/Safari

Pros:

  • Actively developed
  • HTML5/CSS3 support

Cons:

  • Many features not implemented
  • Doesn't support x64 (App must be built for x86)

OpenWebKit is quite nice although many features are not yet implemented, I experienced few issues using it with visual studio which throws null object reference here and then in design mode, there are some js problems. Everyone using it will almost immediately notice js alert does nothing. Events like mouseup,mousedown... etc. doesn't work, js drag and drop is buggy and so on..

I also had some difficulties installing it since it requires specific version of VC redistributable installed, so after exception I looked at event log, found version of VC and installed it.

3. GeckoFX

Pros:

  • Works on mono
  • Actively developed
  • HTML5/CSS3 support

Cons:

  • D̶o̶e̶s̶n̶'̶t̶ ̶s̶u̶p̶p̶o̶r̶t̶ ̶x̶6̶4̶ ̶(̶A̶p̶p̶ ̶m̶u̶s̶t̶ ̶b̶e̶ ̶b̶u̶i̶l̶t̶ ̶f̶o̶r̶ ̶x̶8̶6̶)̶ - see comments below

GeckoFX is a cross platform Webrowser control for embedding into WinForms Applications. This can be used with .NET on Windows and with mono on Linux. Gecko is a layout engine used by Firefox.

I bumped into few information that GeckoFX is not actively developed which is not true, of course it's always one or two versions behind of Firefox but that is normal, I was really impressed by activity and the control itself. It does everything I needed, but I needed some time to get it running, here's a little tutorial to get it running:

  1. Download GeckoFx-Windows-16.0-0.2, here you can check if newer is available GeckoFX
  2. Add references to two downloaded dll's
  3. Since GeckoFX is wrapper you need XulRunner, go to Version List to see which one you need
  4. Now that we know which version of XulRunner we need, we go to Mozilla XulRunner releases, go to version folder -> runtimes -> xulrunner-(your_version).en-US.win32.zip, in our case xulrunner-16.0.en-US.win32.zip
  5. Unzip everything and copy all files to your bin\Debug (or release if your project is set to release)
  6. Go to visual studio designer of your form, go to toolbox, right click inside -> Choose items -> Browse -> Find downloaded GeckoFX winforms dll file -> OK
  7. Now you should have new control GeckoWebBrowser

If your really must use Chrome, take a look at this product called Awesomium, it's free for non-commercial projects, but license is few thousand dollars for commercial.

Community
  • 1
  • 1
formatc
  • 4,195
  • 7
  • 39
  • 76
  • 5
    Very good description of how to get GeckoFx up and running. +1 – Alex Essilfie Jun 09 '13 at 00:15
  • when building Geckofx-Winforms i needed 4 gtk# libraries (atk-sharp, gdk-sharp, glib-sharp and gtk-sharp), so I had to install Mono first. – bernhardrusch Sep 27 '13 at 08:08
  • This answer is spectacular. – Mike Cole Feb 28 '14 at 18:43
  • GeckoFx now supports 64 bit, so just go here: https://bitbucket.org/geckofx/ and then here: http://ftp.mozilla.org/pub/mozilla.org/xulrunner/releases/ and match the runner version up with the latest geckofx version number and you should be good to go – whyoz Feb 23 '15 at 17:06
  • The only thing listed from above that works as an IE drop-in replacement, is IE. The rest are complete disasters. – Kraang Prime Apr 01 '15 at 15:13
  • GeckoFx: Step 5 was not sufficient for me when publishing, and only worked when ran from VS. What did work was copy`omni.ja`, the `.dll`, and the `.exe` files to the same directory as my code files, included them in the project, then setting Build Action for `omni.ja` to content. – Wayne Apr 01 '15 at 16:28
  • 2
    @Wayne if you want to do it right you should create a folder in your project or a speparate project where you would include all files, gecko fx dlls and xul runner files and set them to copy always, then on every build action they would get copied to your build/debug folder or published if you publish from VS, there is also Xpcom.Initialize method that you should call and you can pass it xul runner files path, hope that helps – formatc Apr 01 '15 at 16:43
46

I had the same problem, the WebBrowser was using an old version of IE, with some googling I came across the following code that makes a change into registry and makes the WebBrowser to use the lastest IE version possible:

 public enum BrowserEmulationVersion
    {
        Default = 0,
        Version7 = 7000,
        Version8 = 8000,
        Version8Standards = 8888,
        Version9 = 9000,
        Version9Standards = 9999,
        Version10 = 10000,
        Version10Standards = 10001,
        Version11 = 11000,
        Version11Edge = 11001
    }
    public static class WBEmulator
    {
        private const string InternetExplorerRootKey = @"Software\Microsoft\Internet Explorer";

        public static int GetInternetExplorerMajorVersion()
        {
            int result;

            result = 0;

            try
            {
                RegistryKey key;

                key = Registry.LocalMachine.OpenSubKey(InternetExplorerRootKey);

                if (key != null)
                {
                    object value;

                    value = key.GetValue("svcVersion", null) ?? key.GetValue("Version", null);

                    if (value != null)
                    {
                        string version;
                        int separator;

                        version = value.ToString();
                        separator = version.IndexOf('.');
                        if (separator != -1)
                        {
                            int.TryParse(version.Substring(0, separator), out result);
                        }
                    }
                }
            }
            catch (SecurityException)
            {
                // The user does not have the permissions required to read from the registry key.
            }
            catch (UnauthorizedAccessException)
            {
                // The user does not have the necessary registry rights.
            }

            return result;
        }
        private const string BrowserEmulationKey = InternetExplorerRootKey + @"\Main\FeatureControl\FEATURE_BROWSER_EMULATION";

        public static BrowserEmulationVersion GetBrowserEmulationVersion()
        {
            BrowserEmulationVersion result;

            result = BrowserEmulationVersion.Default;

            try
            {
                RegistryKey key;

                key = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, true);
                if (key != null)
                {
                    string programName;
                    object value;

                    programName = Path.GetFileName(Environment.GetCommandLineArgs()[0]);
                    value = key.GetValue(programName, null);

                    if (value != null)
                    {
                        result = (BrowserEmulationVersion)Convert.ToInt32(value);
                    }
                }
            }
            catch (SecurityException)
            {
                // The user does not have the permissions required to read from the registry key.
            }
            catch (UnauthorizedAccessException)
            {
                // The user does not have the necessary registry rights.
            }

            return result;
        }
        public static bool SetBrowserEmulationVersion(BrowserEmulationVersion browserEmulationVersion)
        {
            bool result;

            result = false;

            try
            {
                RegistryKey key;

                key = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, true);

                if (key != null)
                {
                    string programName;

                    programName = Path.GetFileName(Environment.GetCommandLineArgs()[0]);

                    if (browserEmulationVersion != BrowserEmulationVersion.Default)
                    {
                        // if it's a valid value, update or create the value
                        key.SetValue(programName, (int)browserEmulationVersion, RegistryValueKind.DWord);
                    }
                    else
                    {
                        // otherwise, remove the existing value
                        key.DeleteValue(programName, false);
                    }

                    result = true;
                }
            }
            catch (SecurityException)
            {
                // The user does not have the permissions required to read from the registry key.
            }
            catch (UnauthorizedAccessException)
            {
                // The user does not have the necessary registry rights.
            }

            return result;
        }

        public static bool SetBrowserEmulationVersion()
        {
            int ieVersion;
            BrowserEmulationVersion emulationCode;

            ieVersion = GetInternetExplorerMajorVersion();

            if (ieVersion >= 11)
            {
                emulationCode = BrowserEmulationVersion.Version11;
            }
            else
            {
                switch (ieVersion)
                {
                    case 10:
                        emulationCode = BrowserEmulationVersion.Version10;
                        break;
                    case 9:
                        emulationCode = BrowserEmulationVersion.Version9;
                        break;
                    case 8:
                        emulationCode = BrowserEmulationVersion.Version8;
                        break;
                    default:
                        emulationCode = BrowserEmulationVersion.Version7;
                        break;
                }
            }

            return SetBrowserEmulationVersion(emulationCode);
        }
        public static bool IsBrowserEmulationSet()
        {
            return GetBrowserEmulationVersion() != BrowserEmulationVersion.Default;
        }
    } 

You just need to create a class and put this code in it, then run the following code when the program starts:

 if (!WBEmulator.IsBrowserEmulationSet())
            {
                WBEmulator.SetBrowserEmulationVersion();
            }

VB.NET:

Imports Microsoft.Win32
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Security
Imports System.Text
Imports System.Threading.Tasks

Public Enum BrowserEmulationVersion
    [Default] = 0
    Version7 = 7000
    Version8 = 8000
    Version8Standards = 8888
    Version9 = 9000
    Version9Standards = 9999
    Version10 = 10000
    Version10Standards = 10001
    Version11 = 11000
    Version11Edge = 11001
End Enum


Public Class WBEmulator
    Private Const InternetExplorerRootKey As String = "Software\Microsoft\Internet Explorer"
    Public Shared Function GetInternetExplorerMajorVersion() As Integer

        Dim result As Integer

        result = 0

        Try
            Dim key As RegistryKey
            key = Registry.LocalMachine.OpenSubKey(InternetExplorerRootKey)
            If key IsNot Nothing Then
                Dim value As Object = If(key.GetValue("svcVersion", Nothing), key.GetValue("Version", Nothing))

                Dim Version As String
                Dim separator As Integer
                Version = value.ToString()
                separator = Version.IndexOf(".")
                If separator <> -1 Then
                    Integer.TryParse(Version.Substring(0, separator), result)
                End If
            End If

        Catch ex As SecurityException
            'The user does Not have the permissions required to read from the registry key.
        Catch ex As UnauthorizedAccessException
            'The user does Not have the necessary registry rights.
        Catch

        End Try
        GetInternetExplorerMajorVersion = result
    End Function
    Private Const BrowserEmulationKey = InternetExplorerRootKey + "\Main\FeatureControl\FEATURE_BROWSER_EMULATION"

    Public Shared Function GetBrowserEmulationVersion() As BrowserEmulationVersion

        Dim result As BrowserEmulationVersion
        result = BrowserEmulationVersion.Default

        Try
            Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, True)
            If key IsNot Nothing Then
                Dim programName As String
                Dim value As Object
                programName = Path.GetFileName(Environment.GetCommandLineArgs()(0))
                value = key.GetValue(programName, Nothing)
                If value IsNot Nothing Then
                    result = CType(Convert.ToInt32(value), BrowserEmulationVersion)
                End If
            End If
        Catch ex As SecurityException
            'The user does Not have the permissions required to read from the registry key.
        Catch ex As UnauthorizedAccessException
            'The user does Not have the necessary registry rights.
        Catch

        End Try

        GetBrowserEmulationVersion = result
    End Function
    Public Shared Function SetBrowserEmulationVersion(BEVersion As BrowserEmulationVersion) As Boolean

        Dim result As Boolean = False

        Try
            Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, True)
            If key IsNot Nothing Then
                Dim programName As String = Path.GetFileName(Environment.GetCommandLineArgs()(0))
                If BEVersion <> BrowserEmulationVersion.Default Then
                    'if it's a valid value, update or create the value
                    key.SetValue(programName, CType(BEVersion, Integer), RegistryValueKind.DWord)
                Else
                    'otherwise, remove the existing value
                    key.DeleteValue(programName, False)
                End If
                result = True
            End If
        Catch ex As SecurityException

            ' The user does Not have the permissions required to read from the registry key.

        Catch ex As UnauthorizedAccessException

            ' The user does Not have the necessary registry rights.

        End Try

        SetBrowserEmulationVersion = result
    End Function


    Public Shared Function SetBrowserEmulationVersion() As Boolean
        Dim ieVersion As Integer
        Dim emulationCode As BrowserEmulationVersion
        ieVersion = GetInternetExplorerMajorVersion()

        If ieVersion >= 11 Then

            emulationCode = BrowserEmulationVersion.Version11
        Else

            Select Case ieVersion
                Case 10
                    emulationCode = BrowserEmulationVersion.Version10
                Case 9
                    emulationCode = BrowserEmulationVersion.Version9
                Case 8
                    emulationCode = BrowserEmulationVersion.Version8
                Case Else
                    emulationCode = BrowserEmulationVersion.Version7
            End Select
        End If

        SetBrowserEmulationVersion = SetBrowserEmulationVersion(emulationCode)
    End Function

    Public Shared Function IsBrowserEmulationSet() As Boolean
        IsBrowserEmulationSet = GetBrowserEmulationVersion() <> BrowserEmulationVersion.Default
    End Function
End Class

You may use it like:

If Not WBEmulator.IsBrowserEmulationSet() Then
    WBEmulator.SetBrowserEmulationVersion()
End If
Ashkan Mobayen Khiabani
  • 30,915
  • 26
  • 90
  • 147
  • 1
    Excellent!! Thank you so much! – Ben Jun 06 '16 at 00:23
  • 1
    Translated over to Python (working on an IronPython application) and it worked perfectly. Thanks! – David Jul 13 '16 at 19:30
  • 1
    That saves my life ! Thanks – Neeraj Dubey Nov 17 '17 at 14:51
  • 1
    This is fantastic but is it possible to reset the emulation version? It seems that the version can only be set once in a WinForms application. Whilst it's possible to have it appear to be reset the effect is that, once set, and reset in the registry it is not being re-read with new registry values in a WinForms app. – cymorg Apr 24 '19 at 14:40
  • Does Version11Edge actually use the browser component from Microsoft Edge? or is it just saying to use as new as possible IE like the HTML Meta Tag "Edge"? – DarrenMB May 02 '19 at 02:13
  • 1
    @DarrenMB as far as I know it uses IE11 – Ashkan Mobayen Khiabani Jun 10 '19 at 08:05
  • 1
    Thank you. Is a good solution and makes the webbrowser control much better. – Mark Nov 13 '19 at 14:03
  • I tried this ( a few mods needed for C# operation) but the web site I wish to access doesn't work with IE 11 or Edge or the WebBrowser control using IE 11 or Edge. I guess as Chrome and Firefox dominate the browser market that's all people develop for. – SimonKravis Nov 24 '19 at 09:52
  • if your application does not function as you want (it caused a problem in my associated fields which grab data from the web-browser page), and you decide to go back to your previous version, changes in the browser control (caused I assume by changes to registry values) are still present and cannot be reversed. I suggest before you first try out the Class you create a Restore Point, then if things do not work out restore that saved "Restore Point" to go back to how things were before you started the changes. – Chris Raisin Feb 09 '21 at 07:41
  • Also, since Edge is the standard Browser in Windows now, I would suggest that the line following "If ieVersion >= 11 Then".... be changed from : emulationCode = BrowserEmulationVersion.Version11 to emulationCode = BrowserEmulationVersion.Version11Edge – Chris Raisin Feb 09 '21 at 07:50
28

You can use registry to set IE version for webbrowser control. Go to: HKLM\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION and add "yourApplicationName.exe" with value of browser_emulation To see value of browser_emulation, refer link: http://msdn.microsoft.com/en-us/library/ee330730%28VS.85%29.aspx#browser_emulation

animuson
  • 50,765
  • 27
  • 132
  • 142
golavietnam
  • 301
  • 3
  • 2
  • 11
    And if you're debugging in visual studio, you probably will have to use "yourApplicationName.vshost.exe" – newenglander Jan 23 '12 at 16:27
  • 3
    And if you're debugging in visual studio, you probably can just uncheck "Enable the Visual Studio hosting process" on the Debug tab of your project properties. Then you can use your exe name without "vshost" part. – Daniel Vygolov May 18 '14 at 17:38
  • 2
    @golavietnam Can you please help me with this, I tried your instructions and using the instructions on the link you provided but it still doesn't work. When I visit WhatIsMyBrowser.com using the webbrowser control, it tells me i'm using ie11 BUT in ie**7** compatibility mode and many websites refuse to work and give me a "please upgrade your browser" message. How can I force webbrowser control to use latest version of ie that is installed (ie11) this way doesn't work, I've tried it a hundred times. – jay_t55 Jun 24 '14 at 11:48
21

I know this isn't a 'replacement' WebBrowser control, but I was having some awful rendering issues whilst showing a page that was using BootStrap 3+ for layout etc, and then I found a post that suggested I use the following. Apparently, it's specific to IE and tells it to use the latest variation found on the client machine for rendering (so it won't use IE7, which I believe is the default).

So just put:

<meta http-equiv="X-UA-Compatible" content="IE=Edge" />

somewhere in the head part of your document.

Obviously, if it's not your document, this won't help - though I personally consider it to be a security hole if you're reading pages not created by yourself through the WebBrowser control - why not just use a web browser!

Pang
  • 8,605
  • 144
  • 77
  • 113
  • 2
    If you want this to work on a PC running IE7, don't put it *somewhere* in ``, put it as the very first child of ``. It took us a while to figure this out. – Sylvain May 08 '14 at 13:29
  • 1
    Sorry I meant IE8 not IE7 in my previous comment. So on IE8, if you don't place the `` as the first child of ``, it will run in compatibility mode, not in edge mode. – Sylvain May 08 '14 at 13:57
  • I was using the webbrowser plugin in a winform app and this was a huge help. The page I was hitting was using jquery & angularjs which was choking because it thought the browser was IE7 even though I have IE10 installed. – Kywillis Jul 15 '14 at 15:37
  • That work for me! You can also use this to set to a particular version of IE for example: and always the browser works as 10 (of course you need to have installed the same version or higher – freedeveloper Feb 14 '15 at 07:47
  • This is perfect for my usecase. I want to show nicer html formatted reports. There is lot of CSS content available out there already. This is better approach than embedding chrome/firefox. – MD Luffy Mar 18 '15 at 21:10
  • this worked for me, as I needed to access the web application from inside the browser control, without the browser control throwing all kinds of stuff. – AceMark Sep 12 '15 at 15:22
  • Really thought this had cracked it - good clean bootstrap page, but then hit the javascript errors that halted everything again! Sigh!! – gchq Mar 14 '16 at 22:38
12

UPDATE 2020 JULY

Preview version of chromium based WebView 2 is released by the Microsoft. Now you can embed new Chromium Edge browser into a .NET application.

UPDATE 2018 MAY

If you're targeting application to run on Windows 10, then now you can embed Edge browser into your .NET application by using Windows Community Toolkit.

WPF Example:

  1. Install Windows Community Toolkit Nuget Package

    Install-Package Microsoft.Toolkit.Win32.UI.Controls
    
  2. XAML Code

    <Window
        x:Class="WebViewTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:WPF="clr-namespace:Microsoft.Toolkit.Win32.UI.Controls.WPF;assembly=Microsoft.Toolkit.Win32.UI.Controls"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="clr-namespace:WebViewTest"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="MainWindow"
        Width="800"
        Height="450"
        mc:Ignorable="d">
        <Grid>
            <WPF:WebView x:Name="wvc" />
        </Grid>
    </Window>
    
  3. CS Code:

    public partial class MainWindow : Window
    {
      public MainWindow()
      {
        InitializeComponent();
    
        // You can also use the Source property here or in the WPF designer
        wvc.Navigate(new Uri("https://www.microsoft.com"));
      }
    }
    

WinForms Example:

public partial class Form1 : Form
{
  public Form1()
  {
    InitializeComponent();

    // You can also use the Source property here or in the designer
    webView1.Navigate(new Uri("https://www.microsoft.com"));
  }
}

Please refer to this link for more information.

Pang
  • 8,605
  • 144
  • 77
  • 113
Rahul
  • 1,845
  • 2
  • 28
  • 68
1

Geckofx and Webkit.net were both promising at first, but they didn't keep up to date with Firefox and Chrome respectively while as Internet Explorer improved, so did the Webbrowser control, though it behaves like IE7 by default regardless of what IE version you have but that can be fixed by going into the registry and change it to IE9 allowing HTML5.

user2319683
  • 79
  • 1
  • 7