wpfinternet-explorerclientxbapversion-detection

Detection of browser version in WPF


Is it possible to find out in what browser version a browser hosted application (XBAP) runs (eg. IE6, IE7 or IE8)? I want to find out the browser version from within the XBAP.


Solution

  • With some help from a Microsoft forum I was led into a direction that finally works. Below a proof of concept in C++.NET (.

    using namespace System::Windows::Forms;
    
    [STAThread]
    String^ GetBrowserVersion() {
       String^ strResult = String::Empty;
       WebBrowser^ wb = gcnew WebBrowser();            
       String^ strJS = "<SCRIPT>function GetUserAgent() { return navigator.userAgent; }</SCRIPT>";
       wb->DocumentStream = gcnew MemoryStream( ASCIIEncoding::UTF8->GetBytes(strJS) );            
       while ( wb->ReadyState != WebBrowserReadyState::Complete ) {
          Application::DoEvents();
       }
       String^ strUserAgent = (String^)wb->Document->InvokeScript("GetUserAgent");
       wb->DocumentStream->Close();
       String^ strBrowserName = String::Empty;
       int i = -1;
       if ( ( i = strUserAgent->IndexOf( "MSIE" ) ) >= 0 ) {          
          strBrowserName = "Internet Explorer";
       } else if ( ( i = strUserAgent->IndexOf( "Opera" ) ) >= 0 ) {
          strBrowserName = "Opera";
       } else if ( ( i = strUserAgent->IndexOf( "Chrome" ) ) >= 0 ) {
          strBrowserName = "Chrome";
       } else if ( ( i = strUserAgent->IndexOf( "FireFox" ) ) >= 0 ) {
          strBrowserName = "FireFox";
       }
       if ( i >= 0 ) {
          int iStart = i + 5;
          int iLength = strUserAgent->IndexOf( ';', iStart ) - iStart;
          strResult = strBrowserName + " " + strUserAgent->Substring( iStart, iLength );
       }
       return strResult;
    }