Search code examples
wpfvb.netregistry

Change FEATURE_BROWSER_EMULATION registry key - VB.NET


We have a WPF app that uses the browser control. As this emulates an older version of IE most sites do not render correctly.

By adding a registry key of 'OurApp.exe' and a value of 11000 to HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION\ it then works perfectly.

The question is - how can I detect the current setting (if there is one) and change it if required from within the app using VB.NET?

I can return the current version of IE on the local machine using

Public Function ReturnIEVersion() As String
    Try
        Dim vVersionO As Object = New System.Windows.Forms.WebBrowser().Version
        Dim vVersion As String = vVersionO.ToString
        Return vVersion
    Catch ex As Exception
        EmailError(ex)
        Return "Error"
    End Try
End Function

Then I just need to compare that to any existing entry and update if required, and I assume a method to determine if the local machine is 64 or 32 bit (as the registry paths will be different)?

Thank you


Solution

  • This question is a duplicate. The answer here should resolve your problem!

    I have converted the code to VB for you:

    Private Shared Sub Main()
        If Not mutex.WaitOne(TimeSpan.FromSeconds(2), False) Then
            'another application instance is running
            Return
        End If
        Try
    
            Application.EnableVisualStyles()
            Application.SetCompatibleTextRenderingDefault(False)
    
            Dim targetApplication = Process.GetCurrentProcess().ProcessName + ".exe"
            Dim ie_emulation As Integer = 10000
            Try
                Dim tmp As String = Properties.Settings.[Default].ie_emulation
                ie_emulation = Integer.Parse(tmp)
            Catch
            End Try
            SetIEVersioneKeyforWebBrowserControl(targetApplication, ie_emulation)
    
            m_webLoader = New FormMain()
    
            Application.Run(m_webLoader)
        Finally
            mutex.ReleaseMutex()
        End Try
    End Sub
    
    Private Shared Sub SetIEVersioneKeyforWebBrowserControl(appName As String, ieval As Integer)
        Dim Regkey As RegistryKey = Nothing
        Try
    
    
            Regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", True)
    
            'If the path is not correct or 
            'If user't have priviledges to access registry 
            If Regkey Is Nothing Then
                YukLoggerObj.logWarnMsg("Application FEATURE_BROWSER_EMULATION Failed - Registry key Not found")
                Return
            End If
    
            Dim FindAppkey As String = Convert.ToString(Regkey.GetValue(appName))
    
            'Check if key is already present 
            If FindAppkey = "" + ieval Then
                YukLoggerObj.logInfoMsg("Application FEATURE_BROWSER_EMULATION already set to " + ieval)
                Regkey.Close()
                Return
            End If
    
            'If key is not present or different from desired, add/modify the key , key value 
            Regkey.SetValue(appName, CInt(ieval), RegistryValueKind.DWord)
    
            'check for the key after adding 
            FindAppkey = Convert.ToString(Regkey.GetValue(appName))
    
            If FindAppkey = "" + ieval Then
                YukLoggerObj.logInfoMsg("Application FEATURE_BROWSER_EMULATION changed to " + ieval + "; changes will be visible at application restart")
            Else
                YukLoggerObj.logWarnMsg("Application FEATURE_BROWSER_EMULATION setting failed; current value is  " + ieval)
    
    
    
            End If
        Catch ex As Exception
    
            YukLoggerObj.logWarnMsg("Application FEATURE_BROWSER_EMULATION setting failed; " + ex.Message)
        Finally
            'Close the Registry 
            If Regkey IsNot Nothing Then
                Regkey.Close()
            End If
        End Try
    
    
    End Sub