How to get useragent string for Windows Phone 7?

I need to get the phone user agent string, but I did not find anything in the API that allows this. I came across two blog posts describing the user agent string format:

http://blogs.msdn.com/b/iemobile/archive/2010/03/25/ladies-and-gentlemen-please-welcome-the-ie-mobile-user-agent-string.aspx

http://madskristensen.net/post/Windows-Phone-7-user-agents.aspx

But I did not find a method that can return a user agent. Has anyone been able to do this successfully?

+5
source share
3 answers

WebBrowser script.

.

+3

http://whatsmyuseragent.com .

Samsung Focus: Mozilla/4.0 (compatible: MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; SAMSUNG; SGH-i917)

+4

I made this helper that will create a temporary WebBrowser, load the script, and return the pending userAgent:

internal static class UserAgentHelper
{
    private const string Html = @"<!DOCTYPE html><html><body onload=""window.external.notify(navigator.userAgent);""></body></html>";
    public static Task<string> GetUserAgent()
    {
        var tcs = new TaskCompletionSource<string>();
        var browser = new WebBrowser { IsScriptEnabled = true };
        browser.ScriptNotify += (sender, args) => tcs.SetResult(args.Value);
        browser.NavigateToString(Html);
        return tcs.Task;
    }
}

Using:

var userAgent = await UserAgentHelper.GetUserAgent();

It works at least for WP7.1 and WP8.0:

WP7: "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; Microsoft; XDeviceEmulator)";
WP8: "Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; Microsoft; Virtual)";
0
source